Standard IO process thread functions: fopen, perror, fclose, fprintf, fscanf, fputc, fgetc, buffer, fputs, fgets, fwrite, fread, fseek, ftell,

Standard IO functions:

1. Stream pointer (FILE*):

FILE* fp=fopen(“1.txt”,”r”): This fp is a stream pointer. When this file is opened in read-only mode, a buffer will be applied for at the same time. At this time, the stream pointer fp It will contain the information of the current reading position of the file, if it is readable and writable, it will also contain the information of the data waiting to be read or written in the buffer, and it will also contain whether the file is ended and whether an error occurs.

FILE structure:

struct _IO_FILE{

char* _IO_buf_base; buffer start address

char*_IO_buf_end; buffer end address

int _filen0; file descriptor

}

Buffer size: tail address – first address

Standard stream pointer:

Standard input stream pointer: FILE* stdin

Standard output stream pointer: FILE* stdout

Standard error output stream pointer: FILE* stderr

These three pointers correspond to the terminal file, the stdin stream pointer is used to obtain data from the terminal, and stdout and stderr are used to output data to the terminal. stdout is line-buffered, with a 1024byte buffer, while stderr is unbuffered, with only a 1byte buffer, and one is output one at a time. stderr printing has high real-time performance and is generally used to print error messages to prevent error messages from being unreadable. The scanf, gets, and getchar functions encapsulate the stdin stream pointer by default, and the printf, putchar, and puts functions encapsulate the stdout stream pointer by default.

Standard IO function:

1.fopen(FILE *fopen(const char *pathname, const char *mode);)

Function: used to open a file, return FILE* type pointer if successful, return NULL if failed, update errno.

For example: FILE* fp=fopen(“1.txt”,”r”); Open a file named 1.txt in read-only mode. The first parameter list fills in the path and name of the file to be opened, and the second fills in the opening method.

The opening methods are:

“r”: Read from the beginning, if there is no file, it will fail to open.

“r + “: In the way of reading and writing, read and write from the beginning, no file open failed.

“w”: Open the file in writing mode and start writing from the beginning. If there is no file, create a file and open it. If there is a file, empty the file and open it.

“w + “: Open the file in read-write mode, read and write from the beginning, if there is no file, create and open, if there is a file, clear the file and open.

“a”: Open the file for writing, start writing from the end. If the file does not exist, create a file. If there is a file, the file stream will start from the end, and the original content will not be cleared.

“a + “: Open the file for reading and writing, the file stream is at the beginning. When reading a file, read from the beginning, and when writing a file, start writing from the end of the file.

2.perror(void perror(const char *s);)

Function: According to errno, print error information. The parameter list is filled with prompt strings. For example: output file opening failure information: perror(“fopen:”);

3. fclose(int fclose(FILE *stream);)

Function: close the specified file and release resources (such as buffer). Returns 0 on success, returns EOF (-1) on failure, and updates errno.

For example: FILE*fp=(“1.txt”,”r”);fclose(fp);

4.fprintf(int fprintf(FILE *stream,const char *format,…);)

Function: Format and output the data to the specified file.

For example: a[]=”abcd”;fprintf(fp,”%s”,a); output the string abcd to the file pointed to by the stream pointer fp.

Returns the number of characters to be printed on success, and returns a negative number on failure.

5.fscanf(int fscanf(FILE *stream,const char *format,…);)

Function: Read data from the file format, do not recognize spaces, newlines. Returns the number of read data successfully, and returns EOF (-1) after reading or failure. For example: char a[20]=””;fscanf(fp,”%s”,a); Assign the space in the file or the string before the newline to the string a. Read spaces when the argument is “%c”. The file offset changes when reading.

Using fscanf and fprintf to realize file copying:

 FILE *fp=fopen("2.c","r");
    if(NULL==fp)
    {
        ERR_MSG("fopen");
        return -1;
    }
    FILE *fq=fopen("1.txt","w");
    if(NULL==fq)
    {
        ERR_MSG("fopen");
        return -1;
    }
    char c;
    int res;
    while((res=fscanf(fp,"%c", & amp;c)!=EOF))//copy with fscanf and fprintf
    {
        printf("%c",c);
        fprintf(fq,"%c",c);
    }
    if(fclose(fp)<0)
    {
        ERR_MSG("fclose");
        return -1;
    }
    printf("Close successfully\\
");
    if(fclose(fq)<0)
    {
        ERR_MSG("fclose");
        return -1;
    }
    printf("Close successfully\\
");


6.fputc(int fuptc(int c,FILE *stream);)

Function: Print a single character to the specified file. The integer form of the character is returned successfully, and EOF(-1) is returned on failure. For example: fputc(‘a’,fp);

or fputc(97,fp);

7. fgetc(int fgetc(FILE *stream);)

Function: Read a single character from the specified file. The corresponding integer form is returned successfully, and EOF(-1) is returned on failure. For example: fgetc(fp); extract a single character from the file pointed to by the stream pointer fp, and offset a stream with the size of a single character.

Using fputc and fgetc to realize file copying:

 FILE *fp=fopen("2.c","r");
    if(NULL==fp)
    {
        ERR_MSG("fopen");
        return -1;
    }
    FILE *fq=fopen("1.txt","w");
    if(NULL==fq)
    {
        ERR_MSG("fopen");
        return -1;
    }
    char c;
    while((c=fgetc(fp))!=EOF)//copy with fgetc and fputc
    {
        printf("%c",c);
        fputc(c,fq);
    }
    if(fclose(fp)<0)
    {
        ERR_MSG("fclose");
        return -1;
    }
    printf("Close successfully\\
");
    if(fclose(fq)<0)
    {
        ERR_MSG("fclose");
        return -1;
    }
    printf("Close successfully\\
");

8. Buffer

Standard IO has a buffer, and all data is put into the buffer first, and the buffer will be refreshed only when the buffer is full or certain conditions are met.

Full buffer: the size is 4k. After opening the file manually with fopen, the full buffer is created. Refresh conditions: 1. The buffer is full. 2. The fflush function, forcibly flushes the output stream buffer. 3. Close the stream pointer. 4. The main function calls return. 5. Call the exit function to exit the program. 6. Input and output conversion.

Line buffer: size 1k. The refresh condition is similar to full buffer, and it will be refreshed when \\
is encountered.

Unbuffered: size 1byte, refresh directly, no refresh condition required.

9. fputs(int fputs(const char *s,FILE *stream))

Function: Output the string to the specified file without adding \\
. Returns a non-negative number on success, and returns EOF on failure. No supplement does not mean no recognition.

10. fgets(char *fgets(char *s,int size,FILE *stream))

Function: get the string from the specified file. At most size-1 characters can be obtained, because a \0 position is left after the function stops reading. It will get spaces, \\
, stop reading after encountering \\
, and get \\
. If it succeeds, it will return the first address of the storage data space, and if it fails, it will return NULL.

Using fputs and fgets to realize file copying:

 FILE *fp=fopen("2.c","r");
    if(NULL==fp)
    {
        ERR_MSG("fopen");
        return -1;
    }
    FILE *fq=fopen("1.txt","w");
    if(NULL==fq)
    {
        ERR_MSG("fopen");
        return -1;
    }
    char c[20]="";
    char *p=c;
    int line=0,sum=0;
    while(fgets(c,sizeof(c),fp)!=NULL)//copy with fgets and fputs
    {
        fputs(c,fq);
        sum + =strlen(c);//Calculate the total number of bytes
        for(int i=0;i<strlen(c);i ++ )
        {
            if(*(p + i)=='\\
')
                line + + ;//calculate the number of lines
        }
    }
    printf("There are %d lines, %d byte size",line,sum);
    if(fclose(fp)<0)
    {
        ERR_MSG("fclose");
        return -1;
    }
    printf("Close successfully\\
");
    if(fclose(fq)<0)
    {
        ERR_MSG("fclose");
        return -1;
    }
    printf("Close successfully\\
");

11.fwrite(size_t fwrite(const void *ptr, size_t size, size _t nmember, FILE *stream);)

Function: Write data to the specified file in binary form. Binary form: Split the data into a character form and write it to the file. Parameter list: void *ptr fills in the first address of the specified data to be output. is a generic type pointer. size_t size: How many bytes each data occupies. size_t nmemb: How many data to output. FILE *stream: stream pointer, pointing to the file you want to write. The number of output data is returned successfully, and the number of 0 or less than nmemb is returned on failure.

12. fread(size_t fread(void *ptr, size_t size, size_t nmember, FILE *stream))

Function: Read the binary data from the specified file and convert it into corresponding data. void *ptr: Store the data in a specified location first, and fill in the first address of the corresponding location. size_t size: How many bytes each data occupies. size_t nmemb: How many data to output. FILE *stream: Stream pointer, pointing to the file to read data from. The number of output data is returned successfully, and the number of 0 or less than nmemb is returned on failure.

Use fwrite and fread to realize file copy:

 FILE *fp=fopen("1.c","r");
    if(NULL==fp)
    {
        ERR_MSG("fopen");
        return -1;
    }
    FILE *fq=fopen("copy1.txt","w");
    if(NULL==fq)
    {
        ERR_MSG("fopen");
        return -1;
    }
    char str[2]="";
    while(fread(str,1,1,fp)!=0)
    {
        printf("%s",str);
        fwrite(str,1,1,fq);
    }
    fclose(fp);
    fclose(fq);

13. fseek(int fseek(FILE *stream,long offset,int whence))

Function: Modify the offset of the file to the specified location. FILE *stream: The stream pointer points to the file whose offset you want to modify. whence: There are three specified positions, the beginning of the file: SEEK_SET, the current position of the file: SEEK_CUR, the end position of the file: SEEK_END. long offset: fill in the offset from whence, positive numbers backward, negative numbers forward. The file cannot be offset forward after the beginning, but the offset of the file in the form of “r + ,w,w + ” will add ^@ when the file offset is at the end, and it will be opened in the form of “a,a + ” ^@ will not be added to the backward offset, it will only be written from the last valid character of the file.

14. ftell(long ftell(FILE *stream);)

Function: Return the offset of the beginning of the file, for example, the current position of the file, return -1 if it fails, and update errno. When the file is offset to the end, the obtained value of the file offset is the file size.