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 返回值
发布评论