fscanf每隔一行返回一次(fscanf returns every other line)

编程入门 行业动态 更新时间:2024-10-16 18:36:43
fscanf每隔一行返回一次(fscanf returns every other line)

我正在尝试从名为part_1.txt的文件中读取数字,其中包含以下数据:

101 102 103 104 105 106 107 108 109

这是我正在使用的代码:

#include <stdio.h> int main() { FILE *handle_in1 = fopen("part_1.txt", "r"); FILE *handle_in2 = fopen("part_2.txt", "r"); FILE *handle_out = fopen("parts.txt", "w"); int scan; if(handle_in1 == NULL || handle_in2 == NULL) { printf("File(s) could not be handled."); } else { while(fscanf(handle_in1, "%d", &scan) != EOF) { fscanf(handle_in1, "%d", &scan); printf("Number is %d\n", scan); } } return 0; }

代码应该在屏幕上打印出存储在文件中的每个值,直到文件结束。 相反,它打印出所有其他值(和最后一个值),如下所示:

Number is 102 Number is 104 Number is 106 Number is 108 Number is 109

这段代码有什么问题?

我编辑了这个问题因为我不能在8小时之前发布答案。 以下版本是新版本:

#include <stdio.h> int main() { FILE *handle_in1 = fopen("part_1.txt", "r"); FILE *handle_in2 = fopen("part_2.txt", "r"); FILE *handle_out = fopen("parts.txt", "w"); int scan; if(handle_in1 == NULL || handle_in2 == NULL) { printf("File(s) could not be handled."); } else { fscanf(handle_in1, "%d", &scan); while(fscanf(handle_in1, "%d", &scan) != EOF) { printf("Number is %d\n", scan); } } return 0;

}

这个程序的编辑版本给了我值:102,103,104,105,106,107,108和109.如果我编辑txt文件并在txt文件的顶部放一个额外的101,那么它给出了值从101到109,所以它必须跳过第一个数字。 为什么这样做我不知道......

I'm trying to read numbers from a file named part_1.txt which contains the following data:

101 102 103 104 105 106 107 108 109

This is the code i'm working with:

#include <stdio.h> int main() { FILE *handle_in1 = fopen("part_1.txt", "r"); FILE *handle_in2 = fopen("part_2.txt", "r"); FILE *handle_out = fopen("parts.txt", "w"); int scan; if(handle_in1 == NULL || handle_in2 == NULL) { printf("File(s) could not be handled."); } else { while(fscanf(handle_in1, "%d", &scan) != EOF) { fscanf(handle_in1, "%d", &scan); printf("Number is %d\n", scan); } } return 0; }

The code should print out on the screen every value that is stored in the file, until End Of the File. Instead it prints out every other value (and last value), like this:

Number is 102 Number is 104 Number is 106 Number is 108 Number is 109

What is wrong in this code?

I edited this question since i cannot post answer until 8 hours. The following version is the new version:

#include <stdio.h> int main() { FILE *handle_in1 = fopen("part_1.txt", "r"); FILE *handle_in2 = fopen("part_2.txt", "r"); FILE *handle_out = fopen("parts.txt", "w"); int scan; if(handle_in1 == NULL || handle_in2 == NULL) { printf("File(s) could not be handled."); } else { fscanf(handle_in1, "%d", &scan); while(fscanf(handle_in1, "%d", &scan) != EOF) { printf("Number is %d\n", scan); } } return 0;

}

this edited version of the program gives me the values: 102, 103, 104, 105, 106, 107, 108 and 109. if i edit the txt file and put one extra 101 on top of the txt file, then it gives the values all the way from 101 to 109 so it must skip the first number. Why it does that i have no idea...

最满意答案

你在这里做错了什么:

while(fscanf(handle_in1, "%d", &scan) != EOF) { fscanf(handle_in1, "%d", &scan); printf("Number is %d\n", scan); }

解决方案是:

在循环的迭代中仅使用一个scanf 。

所以它将成为(这是一个解决方案):

while(fscanf(handle_in1, "%d", &scan) != EOF) { printf("Number is %d\n", scan); }

You are doing something wrong over here:

while(fscanf(handle_in1, "%d", &scan) != EOF) { fscanf(handle_in1, "%d", &scan); printf("Number is %d\n", scan); }

The solution is:

Use only one scanf in an iteration of the loop.

So it will become (this is one solution):

while(fscanf(handle_in1, "%d", &scan) != EOF) { printf("Number is %d\n", scan); }

更多推荐

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

发布评论

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

>www.elefans.com

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