【发布时间】:2015-05-23 13:27:45
【问题描述】:
在调试了一段时间后,我意识到C中的函数fgets()有问题:
示例代码:
....
char arr[50];
File* file=fopen("mytext.txt","r");
for(int i=0;i<=100;++i){
....
fgets(arr,sizeof(arr),file); //I use a for loop here for example
.... //just to illustrate that I use the
...do some stuff... //fgets() function lots of times
}
fgets(arr,sizeof(arr),file); //And now I use it again but get nothing
//because I have used the fgets() to consume the file up
....
所以你看,我这里想说的是:当你使用 fgets() 从文件中获取数据的次数太多时,它会到达文件的末尾。然后当你再次使用它时,它什么也不会返回(只是一个空指针但没有错误!!)
我在调试一段时间后发现了这个问题。我注意到像 scanf() 这样的其他 formattd-input 函数也存在同样的问题。
所以我想知道是否有一种方法可以刷新机器并进行更新,以便我可以再次使用 fgets() 函数。 谢谢。
【问题讨论】: