Find the answer to your Linux question:
Results 1 to 3 of 3
main(){ FILE *fp; char a; fp=fopen("giis.txt","r"); if(fp==NULL){ printf("\nFile open error"); return -1; } fscanf(fp,"%c",&a) while(a!=EOF){ printf("%c",a); fscanf(fp,"%c",&a) } close(fp); } Above prints the contents of file and goes into infinite ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation C program -problem with EOF

    main(){
    FILE *fp;
    char a;

    fp=fopen("giis.txt","r");
    if(fp==NULL){
    printf("\nFile open error");
    return -1;
    }

    fscanf(fp,"%c",&a)
    while(a!=EOF){
    printf("%c",a);
    fscanf(fp,"%c",&a)
    }
    close(fp);
    }
    Above prints the contents of file and goes into infinite loop.
    It's not detecting the end of file.

    What's the probllem and How to solve this?
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    fscanf returns EOF when it's reached. It does not put this into the variable "a".
    Debian GNU/Linux -- You know you want it.

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Smile

    Thanks GNU-Fan
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...