Summary of simple knowledge points about fscanf() method and fgets() method in C language

 fscanf()

int fscanf(FILE* stream, const char* format, [argument...]);

The fscanf() method is similar to the scanf() method. The difference is that there is an additional first parameter, the file pointer parameter, and the usage of the last two parameters is similar. It should be noted that when filling in the variable in the third parameter, do not omit the address character & .

main point

        1. format str: such as %d, %f, %c, %s, etc., respectively means to read an integer, floating point number, character, string. You can also add control, such as %ld, which means to read a long integer, and %20s, which means to read up to 20 characters .
        2. Return value: If there is no error, fscanf returns the number of correctly matched and assigned fields; if an error occurs, it returns EOF.

        3. In the sense of blanks, fscanf treats blanks, tabs, and newlines equally without distinction; %s will skip the preceding blanks, but will not skip the following blanks; % c does not skip whitespace .
        4. * means to read a field, but not assign it to a variable.
        5. [] means to read only the characters in the brackets, [] means not to read the characters in the brackets, it is worth noting that %[]s will not skip the preceding blanks.
        6. If the end of the file stream is reached before any field matching is successful or any matching failure occurs, an error occurs; or an error occurs while reading the file stream. In both cases, fscanf returns EOF.

For example, when summing the numbers in the above files, when using the fscanf() method, the performance is one-to-one when judging spaces and newlines or tabs.

The processing results are as follows

#include<stdio.h>
int main(){
	FILE* fp = NULL; 
	fp = fopen("测试文件01.txt","r");
	int sum =0;int temp =0;
	while(fscanf(fp,"%d",&temp)!=EOF){
		printf("当前数字为: %d\n",temp); 
		sum += temp;
	}
	printf("这是文件中数字总和: %d",sum);
	fclose(fp);
	return 0;
} 

It should be noted here that the while loop condition cannot only fill in fscanf(fp,"%d",&temp) otherwise it will enter an infinite loop! The most standard is fscanf(fp,"%d",&temp)!=EOF Remember to be !=EOF

fgets()

char *  fgets(char * s, int n,FILE *stream);

Because the fgets() method is to get a line, the first parameter is the character array, the temporary storage address of the read string. Essentially pointers. The second parameter indicates that a maximum of n-1 characters can be read each time. If the number of characters in a line is greater than n, it will be output in multiple times, but the next line will not be read until all the lines are output. The third parameter is the file pointer.

main point 

        fgets will end when it encounters a carriage return. It will be entered into the buffer without any conversion of spaces and carriage returns. After the end, write one more \0 to the buffer, so it reads a line of data

When reading this file, when the second parameter is 5, the result is as follows:

If the parameter is changed to 6, the result is as follows:

#include<stdio.h>
int main(){
	FILE* fp = NULL; 
	fp = fopen("测试文件02.txt","r");
	int sum =0;int temp =0;
	char sd[100];
	while(fgets(sd,6,fp)!=NULL){
		printf("当前数字: %s\n",sd);
	} 
	return 0;
} 

1. When fgets encounters a "space", it reads it as it encounters an ordinary character, and when it encounters a "carriage return", it ends this reading, and finally adds a "\" to the end of the buffer (char *buf[]) 0" means the end of reading a line this time.

2. Regardless of whether fscanf encounters a "space" or a "carriage return", it will be read as '\0' into the buffer (char *buf[]), and this reading will end.

3.        Pay attention to the difference in conditions in the while loop when getting all the content in the file!

         fgets() cannot be used!=EOF, you can use fgets(sd,6,fp)!=NULL or fgets(sd,6,fp)

         fscanf() cannot use the above two methods, but can use !=EOF.

(If there is a mistake, please correct me!)

Guess you like

Origin blog.csdn.net/weixin_49418695/article/details/123384870