我想继续读文件直到它结束,但它输出错误答案并且不能停止。 我的代码是:为什么我的代码不能停止?使用fscanf
#include <stdio.h>
#include <stdlib.h>
void create() {
FILE *fin = fopen("in", "r");
int temp[4], t;
int aaa;
while ((aaa = scanf(fin, "%d:%d:%d:%d/%d",
&temp[0], &temp[1], &temp[2], &temp[3], t)) != EOF) {
printf("%d\n", aaa);
printf("%d:%d:%d:%d\n", temp[0], temp[1], temp[2], temp[3]);
}
fclose(fin);
}
int main() {
create();
return 0;
}
和我的输入文件的内容是:
103.29.43.0/24
103.29.5.0/24
103.29.52.0/22
103.29.56.0/22
103.29.60.0/22
103.2.96.0/22
103.29.64.0/24
103.29.65.0/24
103.29.66.0/24
103.29.67.0/24
103.29.7.0/24
但它输出:
0
103:0:0:0
0
103:0:0:0
0
103:0:0:0
....(loop)
比较'的fscanf()'〜5,而不是'EOF'。 –
请不要在评论和答案中修正所提出的修补程序中的代码,否则会导致不一致。 – chqrlie