开发者

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

开发者 https://www.devze.com 2023-03-10 12:17 出处:网络
void OpenFile() { FILE *fp; char buffer[1024]; int number; fp=fopen(\"godess.txt\",\"r\"); if(fp==NULL){ printf(\"Error opening file!\\n\");
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 everythin开发者_开发问答g...but nothing happens!!!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消