通常fscanf,当使用 扫描非整数时%d,将失败,直到从输入流中显式删除非整数字符。尝试扫描a123失败,直到a从输入流中删除。
尝试扫描------123失败(fscanf返回0),但-已从输入流中删除。
这是正确的行为吗fscanf?
该文件包含----------123此代码的结果:
#include <stdio.h>
int main(void) {
int number = 0;
int result = 0;
FILE *pf = NULL;
if (NULL != (pf = fopen("integer.txt", "r"))) {
while (1) {
if (1 == (result = fscanf(pf, "%d", &number))) {
printf("%d\n", number);
} else {
if (EOF == result) {
break;
}
printf("result is %d\n", result);
}
} …Run Code Online (Sandbox Code Playgroud) c ×1