fscanf 返回值

编程入门 行业动态 更新时间:2024-10-28 03:28:04
本文介绍了fscanf 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

What does fscanf return when it reads data in the 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.

推荐答案

来自 手册页Xscanf 函数族:

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

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

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

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.

更多推荐

fscanf 返回值

本文发布于:2023-10-26 14:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:返回值   fscanf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!