Common functions of files

1 fprintf

​​​​​​​Insert picture description here

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

Insert picture description here
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

Insert picture description here

4 fread

Insert picture description here

5 fwrite

Insert picture description here

6 fseek

Insert picture description here

7 fgets

Insert picture description here

Read by line

8 fputs


Write by line

9 feof

Insert picture description here
Used to determine whether the end of the file has been reached

10 fclose

Insert picture description here

11 fflush

Insert picture description here
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

Guess you like

Origin blog.csdn.net/qq_43496435/article/details/113776923