我有一个应该读取文件并检测文件何时结束的函数。
该功能当前只读取倒数第二行并结束。有人可以告诉我我做错了什么,因为我自己也看不到:
int readIn(TinCan* inCan, int toggle)
{
int ii, isFinished = 0;
char fullName[20];
sprintf(fullName, "Label_%d.txt", inCan->pid);
FILE* fp;
fp = fopen(fullName, "r");
if(fp==NULL)
{
printf("Error: could not open %s\n", fullName);
}
else
{
for (ii=0; ii < ((inCan->ac)-1); ii++)
{
fscanf(fp, "%*d %*d %*d\n"); /*move through lines without scanning*/
}
fscanf(fp,"%d %d %d", &inCan->ac, &inCan->state, &inCan->time);
}
if (feof(fp) && (toggle == 1))
{
printf("File ended");
writeLog(inCan);
isFinished = 1;
terminated++;
}
fclose(fp);
return finished;
}
答案 0 :(得分:1)
我希望你的程序有一个循环,类似于:
while (!feof(fp)) {
...
fscanf(fp, "%*d %*d %*d\n");
...
}
如果要检测文件何时结束。