C语言 fscanf 返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15318360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:39:17  来源:igfitidea点击:

fscanf return value

cscanf

提问by slow

What does fscanf return when it reads data in the file. For example,

fscanf 在读取文件中的数据时返回什么。例如,

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.

我只想知道为什么它会根据参数的数量返回这样的值。

回答by Charles Salvia

From the manpage for the Xscanf familyof functions:

Xscanf 系列函数的联机帮助页

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

成功完成后,这些函数将返回成功匹配和分配的输入项的数量;在早期匹配失败的情况下,此数字可以为零。如果输入在第一次匹配失败或转换之前结束,则应返回 EOF。如果发生读取错误,则设置流的错误指示符,应返回 EOF,并应设置 errno 以指示错误

So your first call to fscanfreturns 1 because one input item (&number1) was successfully matched with the format specifier %d. Your second call to fscanfreturns 4 because all 4 arguments were matched.

因此,您第一次调用fscanf返回 1,因为一个输入项 ( &number1) 与格式说明符成功匹配%d。您第二次调用fscanf返回 4,因为所有 4 个参数都匹配。

回答by Barath Ravikumar

I quote fromcplusplus.com.

我从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.

成功时,该函数返回成功填充的参数列表的项目数。由于匹配失败、读取错误或到达文件末尾,此计数可能与预期的项目数匹配或更少(甚至为零)。

如果在读取时发生读取错误或到达文件末尾,则会设置正确的指示符(feof 或 ferror)。并且,如果在任何数据可以成功读取之前发生任何一种情况,则返回 EOF。

--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);

回答by nitin

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.

这恰好是一个非常直接的问题,查尔斯和我之前的 ed 已经恰当地回答了这个问题。但是他们没有提到下次遇到困难时应该在哪里寻找此类东西。

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.

首先是问题 - fscanf 属于格式化输入(扫描)函数系列,这些函数应该读取输入并报告有关读取的数据的一些信息,例如字节或获得适当输入读取的项目(变量地址)的计数并成功分配。

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).

这里 fscanf 应该使用函数调用中提供的格式字符串检查输入文件中的匹配项,并相应地分配(按它们的位置顺序)变量 - 地址与值,一旦完成,它将返回总计数它所做的成功分配的数量。因此 1 和 next 的结果是 4(假设输入正确)。

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.

第二部分:在哪里看?-- 如果您参考了手册页或 posix doc,可以很容易地找到此类功能的详细描述。

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

如果您注意到,前两个答案还包含手册页的小节选。

hope this helps.

希望这可以帮助。

回答by Hitesh Menghani

The return value is not depending on the number of arguments to fscanf,it depends on number of values successfully scanned by fscanf.

返回值不取决于参数的数量fscanf,它取决于成功扫描的值的数量fscanf

回答by Ed Heal

From the manualpage:

手册页:

*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.

因此,如果能够从文件中读取一个整数,则第一个返回 1,如果能够从文件中读取 4 个整数,则第二个返回 4。