Reading a C file, read an extra line, why?

前端 未结 6 1885
余生分开走
余生分开走 2020-12-21 05:12

I don\'t know exactly why a file pointer reads an extra line from a file, specifically the last line, here is the code:

FILE *fp ;
fp = fopen (\"mac_ip.txt\"         


        
6条回答
  •  眼角桃花
    2020-12-21 05:43

    Your code resembles to the following example

    #include 
    
    int main(void)
    {
        char buffer[256];
        FILE * myfile;
    
        myfile = fopen("some.txt","r");
    
        while (!feof(myfile))
        {
            fgets(buffer,256,myfile);
            printf("%s",buffer);
        }
    
        fclose(myfile);
    
        return 0;
    }
    

    from

    http://www.friedspace.com/feof.html

提交回复
热议问题