1. Read string file
- Read string function fgets()
Function prototype:
char *fgets(char *str,int n,FILE *fp); //Its meaning is to read 10 characters from the file referred to in fp, send them into the character array STR, and then print out the str string. If the file cannot read data, it will return NULL and leave the loop at this time. FILE *fp; char str[10]; fp=fopen("E:\\code\\1.txt","rt"); while((fgets(str,10,fp)!=NULL){ printf("os",str); }
Its function is to read n characters at the file position indicated by the file pointer and put them into the str character array. NULL if the string cannot be read.
- Write string function (fputs)
Function prototype:
int fputs(char *str,FILE *fp); //Its meaning is to write the contents of the string str to the file indicated by the file pointer fp. FILE *fp; char str[10]; fp=fopen("E:\\code\\1.txt","rt"); gets(str); fputs(str,fp);
Its function is to write the string str to the position of the file indicated by the file pointer fp. A non-zero value is returned when writing data is successful, and EOF is returned when writing fails. The string str can be a string constant, a character array name or a pointer variable.
2. Read / write function of formatted string
- Format string read function (fscanfO)
Function prototype:
int fscanf(FILE *fp,"Format string ", enter the item address table); //The following data is stored in 1.txt under the code folder of disk E. the data includes student number, name and gender. Now read the variables specified in this data from this file FILE *fp; char num[20],name[40],sex[5]; fp=fopen("E:\\code\\1.txt","t"); fscanf(fp,"%s %s %s",num,name,sex);
Function: send the data in the file to the input item address table according to the format specified by the format string from the file pointed to by the file pointer fp. If the data is read successfully, the number of read data will be returned, and the data will be stored in the variable or array in the memory according to the specified format, and the file pointer will automatically move downward; EOF is returned if reading fails.
- Format string write function fprintf()
Function prototype:
int fprintf(FILE *FP,"format string ",Output item address table); //First, initialize the data to be appended, and then open the specified file in append write mode. char num[20]="20210101",name[40]="so-and-so",sex[5]="male"; FILE *fp; fp=fopen("E:\\code\\1.txt","a+"); fputc('\n',fp); /*Write a carriage return line feed to keep the original file format unchanged*/ fprintf(fp,"%s %s %s",num,name,sex);
Function: output the variable value in the output item table to the file position pointed by the file pointer fp according to the format specified by the format string
3. Data block read / write operation
- Block read function (FREAD)
Function prototype:
int fread(void *buffer,int size,int count,FILE* fp); function fread()The meanings of the parameters in the are as follows: (1) buffer Is a pointer,It represents the first address where the read data is stored(Where is it stored). (2) size Represents the number of bytes of the data block. (3) count Indicates the number of data blocks to read and write. //Its meaning is to read 4 bytes (a real number) each time from the file referred to by fp into the real array fa for 5 consecutive times, that is, read 5 real numbers into fa float fa[5]; fread(fa.4,5,fp);
Function: Start * * from the current position of the file pointed to by the file pointer fp, read size bytes at a time, repeat * *, store the read data in the memory area starting from buffer, and move the read-write position pointer back size*count times. The return value of this function is the actually read count value.
- Block write function fwrite()
Function prototype:
int fwrite(void *buffer,int size,int count, FILE* fp); function fwrite()The meanings of the parameters in the are as follows: (1) buffer Is a pointer that represents the first address of the data to be output in memory(That is, where to start storage). (2) size Represents the number of bytes of the data block. (3) count Indicates the number of data blocks to read and write //Its meaning is to read and write 4 bytes (a real number) from the fa real array to the file indicated by fp for 5 consecutive times, that is, write 5 real numbers to the file indicated by fp. float fa[5]; fwrite(fa.45,fp);
Function: starting from the memory area pointed to by the buffer, output size bytes at a time, repeat count times, store the output data in the file pointed to by fp, and move the read-write position pointer back size*count times.
4. Random reading of files
- Function rewind()
Function prototype:
void rewind(FILE *fp);
Its function is to move the position pointer inside the file to the beginning of the file.
- Function fseek()
Function prototype:
int fseek(FILE *fp, long offset,int whence); The second parameter is the offset(offset). This parameter represents the distance to be moved from the starting point(See table)Starting point mode). The parameter must be a 1 ong Value of type, which can be positive(Move forward),negative(Move back)Or 0(Hold still) The third parameter is the mode, which determines the starting point.
Its function is to move the file pointer from the where address to the offset address.
pattern | Offset start position |
---|---|
SEEK_SET | Beginning of document |
SEEK_CUR | current location |
SSEK_END | end of file |
Here is the call fseek()Some examples of functions, fp Is a file pointer: fseek(fp, 0, SEEK SET); //Navigate to the beginning of the file fseek(fp,10,SEEK_SET); //Navigate to the 10th byte in the file fseek(fp, 2,SEEK CUR); //Move 2 bytes forward from the current location of the file fseek(fp, 0,SEEK_END); //Navigate to the end of the file fseek(fp,-10,SEEK_END);//Rewind 10 bytes from the end of the file
- Function ftell()
The function ftell) is used to get the current position of the streaming file, expressed by the displacement relative to the beginning of the file.
If the return value of the ftell function is - 1L, it indicates an error. For example:
i=ftell(fp); if(i==-1L){ printf("Error!\n"); } variable i Stored in the current location, such as an error in calling the function(If this file does not exist),Then output“ Error!".
5. Error check
- Function feof()
Function prototype:
int feof(FILE *fp);
Function: judge whether the file pointer fp is at the end of the file. If the file ends, the return value is 1, otherwise it is 0.
- Function ferror()
Function prototype:
int ferror(FILE *fp);
Function: check whether there is an error when reading and writing the file with various input / output functions. If the return value of ferror is 0, it means there is no error, otherwise it means there is an error.
It should be noted that every time the input / output function is called for the same file, a new ferror function value is generated. Therefore, the initial value of the ferror function should be automatically set to 0 when the fopen function is executed.
- Function clearerr()
Function prototype:
int clearerr(FILE *fp);
Function: used to clear the error flag and end of file flag to 0. Assuming an error occurs when calling an input / output function, the value of ferror function is a non-0 value. After calling clearerr(fp), the value of ferror(fp) becomes 0.
Whenever an error flag appears, it remains until the clearerr function or rewind function, or any other input / output function, is called on the same file.