What does fscanf return when it reads data in the file. For example,
int number1, number2, number3, number4, c;
c = fscanf (spFile, \"%d\", &number1);
/
From the manpage for the Xscanf family of functions:
Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned, and errno shall be set to indicate the error
So your first call to fscanf
returns 1 because one input item (&number1
) was successfully matched with the format specifier %d
. Your second call to fscanf
returns 4 because all 4 arguments were matched.
I quote from cplusplus.com .
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.
--EDIT--
If you are intention is to determine the number of bytes read to a string.
int bytes;
char str[80];
fscanf (stdin, "%s%n",str,&bytes);
printf("Number of bytes read = %d",bytes);
From the manual page:
*These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure. *
Hence 1st one returns 1 if able to read one integer from the file, 2nd one returns 4 if able to read 4 integers from the file.
The return value is not depending on the number of arguments to fscanf
,it depends on number of values successfully scanned by fscanf
.
This happens to be a very straight forward question , and has been aptly answered by charles and ed before me. But they didnt mention where you should be looking for such things the next time you get stuck.
first the question -- the fscanf belongs to the family of formated input(scan) functions that are supposed to read a input and report some info on the data read like bytes or the count of items(variable addresses) that got a appropriate input read and had successfull assignment made.
here the fscanf is supposed to check for matches in the input file with the format string provided in the function call and accordingly assign the (in order of their position) variable - address with the value and once completed it will return the total count for the number of successfull assignments it made. hence the result of 1 and next was 4 (assuming input was provided properly).
second part: where to look ? -- well described details for such function are easily found in your manual pages or posix doc if you refer to one.
if you noticed , the previous two answers also contain small extracts from the man pages .
hope this helps.