How to use feof(FILE *f)?

后端 未结 3 1833
野的像风
野的像风 2020-12-06 22:55

I\'m having a hard time with a do-while loop, that is supposed to stop when we reach the end of the file. Here\'s the loop code:

do  {
    if (pcompanyRow[0]         


        
3条回答
  •  萌比男神i
    2020-12-06 23:56

    I suggest use both fgets() and feof(). Last string in file might have \n or might not. If you use only feof(), you can skip (lost) last line.

     for(;;)
     {char *pc;
    
      pc=fgets(buf,sizeof(buf),fd);
    
      if(!pc)
        {//it may be read error or end of file
          if(feof(fd))
            {//it is end of file, normal exit from for
             break;      
            }
           else
            {//it is i/o error 
             fclose(fd);
             return 2;
            }
        }
    }//for
    

提交回复
热议问题