C language file operations of the open file with read and write files

A, fopen

Function prototype: FILE * fopen (const char * filename, const char * mode);

Return Value: The return value of type FILE *, pointing to the successful return to open the file to open the file pointer, open the file failed to return a null pointer (NULL)

Code Example:

#include <stdio.h>

void OpenFile (FILE ** map); // Open the file
void JudgeOpenSuc (FILE * judge); // determine whether the file is opened successfully

int main()
{
    FILE *fp;

    OpenFile(&fp);
    JudgeOpenSuc(fp);

    return 0;
}

void OpenFile(FILE **map)
{
    (*map) = fopen("E:my.txt", "a+");
}

void JudgeOpenSuc(FILE *judge)
{
    if (judge != NULL)
    {
        printf("Open successfully\n");
    }
    else
    {
        printf("Open failure\n");
    }
}

二、fopen_s

The function prototype: errno_t fopen_s (FILE ** pFile, const char * filename, const char * mode);

Return Value: The return value type bits errno_t, open the file successful return 0, open the file failed to return a nonzero

Code Example:

#include <stdio.h>

const int SUC = 0;

void OpenFile (FILE ** map, errno_t * err); // Open the file
void JudgeOpenSuc (errno_t err); // determine whether the file is opened successfully

int main()
{
    FILE *fp;
    errno_t err;

    OpenFile(&fp, &err);
    JudgeOpenSuc(err);

    return 0;
}

void OpenFile(FILE **map, errno_t *err)
{
    (*err) = fopen_s(map, "E:my.txt", "a+");
}

void JudgeOpenSuc(errno_t err)
{
    if (err == SUC)
    {
        printf("Open successfully\n");
    }
    else
    {
        printf("Open failure\n");
    }
}

Three, _wfopen

Function prototype: FILE * _wfopen (const wchar_t * filename, const wchar_t * mode);

Return Value: The return value of type FILE *, pointing to the successful return to open the file to open the file pointer, open the file failed to return a null pointer (NULL)

Code Example:

#include <stdio.h>

void OpenFile (FILE ** map); // Open the file
void JudgeOpenSuc (FILE * judge); // determine whether the file is opened successfully

int main()
{
    FILE *fp;

    OpenFile(&fp);
    JudgeOpenSuc(fp);

    return 0;
}

void OpenFile(FILE **map)
{
    (*map) = _wfopen(L"E:my.txt", L"a+");
}

void JudgeOpenSuc(FILE *judge)
{
    if (judge != NULL)
    {
        printf("Open successfully\n");
    }
    else
    {
        printf("Open failure\n");
    }
}

Four, _wfopen_s

函数原型:errno_t _wfopen_s( FILE** pFile, const wchar_t *filename, const wchar_t *mode );

Return Value: The return value type bits errno_t, open the file successful return 0, open the file failed to return a nonzero

Code Example:

#include <stdio.h>

const int SUC = 0;

void OpenFile (FILE ** map, errno_t * err); // Open the file
void JudgeOpenSuc (errno_t err); // determine whether the file is opened successfully

int main()
{
    FILE *fp;
    errno_t err;

    OpenFile(&fp, &err);
    JudgeOpenSuc(err);

    return 0;
}

void OpenFile(FILE **map, errno_t *err)
{
    (*err) = _wfopen_s(map, L"E:my.txt", L"a+");
}

void JudgeOpenSuc(errno_t err)
{
    if (err == SUC)
    {
        printf("Open successfully\n");
    }
    else
    {
        printf("Open failure\n");
    }
}

五、fscanf、fgetc、fgets、fscanf_s

fscanf()

Function Prototype: int fscanf (FILE * fp, const char * format, ......);

Return Value: the number of parameters in the parameter list to be read successfully

Code Example:

char ch;
fscanf(fp, "%c", &ch);

 

fgetc()

Function Prototype: int fgetc (FILE * stream);

Return Value: successfully read value is determined corresponding to the character reading an int (note the int type, if used to receive the return value of type char variable truncation may result in data), read failure or EOF

Code Example:

1 int ch;
2 ch = fgetc(fp);

 

fgets()

Function prototype: char * fgets (char * str, int numChars, FILE * stream);

Returns: the array of characters first address when reading success, that is str; return NULL when reading failure

Code Example:

char *p;
char ss[20];

p = fgets(ss, 20,fp);
if (p != NULL)
{
    printf("%s", ss);
}

fscanf_s()

Function Prototype: int fscanf_s (FILE * stream, const char * format [, argument] ...);

Returns: Returns the number of parameters successfully read

Code Example:

1 char ss[20];
2 int k;
3
4 k = fscanf_s(fp, "%s", ss, _countof(ss));
5 printf("%s", ss);

 

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159741.htm