Problem with reading file…while (!feof(file)) leads to infinite loop!

会有一股神秘感。 提交于 2019-12-11 10:13:50

问题


void OpenFile() {

    FILE *fp;

    char buffer[1024];
    int number;
    fp=fopen("godess.txt","r");
    if(fp==NULL){
        printf("Error opening file!\n");
        exit(0);
    }
    else {  
            while (!feof(fp)) {
                printf("In loop!!!!");
                fscanf(fp,"%d\n",&number);
            }
            fclose(fp);
    }
}

Hello...I'm trying to read a file ...but in the line where I write " while (!feof(fp))"....it leads in an infinite loop....can anyone please help me to solve this matter..Please I have tried everything...but nothing happens!!!


回答1:


You should check the return value of fscanf to see whether it has successfully read a number.



来源:https://stackoverflow.com/questions/6231615/problem-with-reading-file-while-feoffile-leads-to-infinite-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!