【发布时间】:2017-05-30 04:19:19
【问题描述】:
我是 C 语言的初学者。在这里,我想从文件*fileptrIn 中读取数据并进行一些计算,并将答案存储在*fileptrOut 中。但是我得到了一个无限循环,文件中的第一个元素是 *fileptrIn。它仅在终端中重复打印文件 *fileptrIn 中的第一个元素。由于我没有收到任何编译错误,因此无法检测到错误。有什么建议可以编辑我的代码吗?
#include<stdio.h>
int main(void)
{
int value;
int total = 0;
int count = 0;
FILE *fileptrIn;
fileptrIn = fopen("input.txt", "r");
if(fileptrIn == NULL)
{
printf("\nError opening for reading.\n");
return -1;
}
printf("\nThe data:\n");
fscanf(fileptrIn, "%d", &value);
while(!feof(fileptrIn))
{
printf("%d", value);
total += value;
++count;
}
fclose(fileptrIn);
return 0;
}
【问题讨论】: