Fscanf return value

Which returns fscanf when reading data in a file. For example,

int number1, number2, number3, number4, c; c = fscanf (spFile, "%d", &number1); //c will be 1 in this case. c = fscanf (spFile, "%d %d %d %d", &number1, &number1, &number3, &number4); //in this case, c will return 4. 

I just want to know why it returns such values ​​depending on the number of arguments.

+8
c scanf
source share
5 answers

From the man page for the Xscanf family of functions:

Upon successful completion, these functions should return the number of successfully matched and assigned input elements ; this number may be zero in case of early non-compliance. If the input ends earlier than the first matching failure or conversion, an EOF is returned. If a read error, an error indicator is set for the stream, EOF should be returned, and errno - indicate an error

So, your first call to fscanf returns 1, because one input element ( &number1 ) was successfully mapped to the %d format specifier. The second call to fscanf returns 4, since all 4 arguments were matched.

+8
source share

I quote cplusplus.com .

If the function succeeds, the function returns the number of elements in the argument. This score may correspond to the expected number of elements or less (even zero) due to a mismatch, error indication, or reach of the end of the file.

If a reading error occurs or the end of the file is reached, reading, the correct indicator is set (feof or ferror). And if occurs before any data can be successfully read, EOF is returned.

- EDIT -

If you intend to determine the number of bytes read per line.

 int bytes; char str[80]; fscanf (stdin, "%s%n",str,&bytes); printf("Number of bytes read = %d",bytes); 
+3
source share

From the manual :

* These functions return the number of successfully entered and assigned input elements, which may be less than provided, or even zero in case of an early match. *

Therefore, the 1st returns 1 if it is able to read one integer from the file, the second returns 4 if it is able to read 4 integers from the file.

+1
source share

This turns out to be a very direct question, and it was definitely received by Charles and published before me. But they did not mention where you should look for such things the next time you get stuck.

first the question is - fscanf belongs to the family of built-in input (scan) functions that should read the input data and report some information about the data read as bytes, or the number of elements (address variables) that received the appropriate input reading and had a successful assignment.

here fscanf should check for matches in the input file with the format string provided in the function call, and accordingly assign (in the order of their position) the address variable with the value and, after its completion, returns, count the number of successful jobs that he did. therefore, result 1 and the next was 4 (provided that the input was provided properly).

second part: where to look? - Well-described details for such a function can easily be found in your manual pages or posix doc if you link to one.

if you noticed, the previous two answers also contain small excerpts from the man pages.

hope this helps.

+1
source share

The return value does not depend on the number of arguments before fscanf , it depends on the number of successfully scanned fscanf values.

+1
source share

All Articles