table of Contents
1 fprintf
The difference between fprintf and printf:
The data flow direction is all program -> buffer
The buffer is different. The buffer of fprintf is a normal file stream, and the buffer of printf is standard output.
2 fscanf
The difference between fscanf and scanf:
Data flow direction is buffer -> program
The buffer is different, the buffer of fscanf is a file stream
Stop reading buffer when encountering'\n'
usage:
The format of each line of the file: student ID\t name\t gender\t score 1\t score 2\t score 3\n
typedef struct stu
{
int num;
char name[100];
char gender;
int score1;
int score2;
int score3;
}Stu_t,*pStu_t;
while (!feof(fp))
{
pStu_t p = (pStu_t) calloc(1 , sizeof(Stu_t));
fscanf(fp , "%d\t%s\t%c\t%d\t%d\t%d" , &p->num , p->name , &p->gender , &p->score1 , &p->score2 , &p->score3);
}
3 fopen
4 fread
5 fwrite
6 fseek
7 fgets
Read by line
8 fputs
Write by line
9 feof
Used to determine whether the end of the file has been reached
10 fclose
11 fflush
Is to write the contents of the file buffer back to disk. Because you need to read and write to the disk many times, use as little as possible
In addition to fflush can be written back to disk, the following operations are also possible:
When the file stream is full, it will automatically write back to disk.
Executing fclose will also write the contents of the buffer back to disk
12 ftell
ftell (fp): display the current reading and writing pointer position of the file