1
所以我试图编写一个函数来读取文本文件中的行数。但是,我注意到即使超出我的文本文件中的行数,该函数也不会退出。为什么?为什么它的工作,当我把为什么我的阅读文本文件中的行数不行?k
fgets(sentence, 70, inputFile);
柜台前++
int GetNumLine(char *fileName){
FILE *inputFile;
//counter are used to store number of lines
int counter = 0;
char sentence [70];
inputFile = fopen(fileName,"r");
//if there are anything wrong with inputfile
if(inputFile == NULL){
printf("Error while opening file");
exit(1);
}
while(!feof(inputFile)){
counter++;
}
fclose(inputFile);
return counter;
}
你没有对while循环中的文件指针做任何事情。 'fgets()'读取一行并前进指针。 – 2015-02-23 02:46:49
因为您从未从文件中读取任何内容,所以您始终保持着! – immibis 2015-02-23 02:55:53