gpt4 book ai didi

c - 如何停止 CSE 不要重复 CSE

转载 作者:行者123 更新时间:2023-11-30 19:32:47 24 4
gpt4 key购买 nike

#include <stdio.h>
#define BUFFER_SIZE 80
int main()
{
FILE* fp = fopen("fp.txt" ,"r");
char buf[BUFFER_SIZE];
if(fp!=NULL)
{
while (!feof(fp))
{
fscanf(fp ,"%s",buf);
printf("Read line: %s\n" ,buf);\
printf("\n");
}
}
fclose(fp);
}
//need create fp file
UW
CSE

如何停止 CSE 不要再次重复 CSE。您可以使用条件 if-else do-while 等吗?

最佳答案

请参阅Why is “while ( !feof (file) )” always wrong?函数 feof 并不像许多初学者想象的那样。

之所以重复输入CSE,是因为在读取超出文件末尾后,该数据仍保留在缓冲区中。

我建议你将循环更改为

while (fscanf(fp, "%79s", buf) == 1) {
printf("Read line: %s\n", buf);
}

关于c - 如何停止 CSE 不要重复 CSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46702381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com