Thread: Check for EOF

  1. #1
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123

    Check for EOF

    How can I read info from a file and end when the file contains no more info?

    I am currently using a '!' (inside the file) on its own to signify the end of the file but this is causing later problems.

    I have a vague idea I need to use 'EOF' but I have no idea how to check for this... any help would be great... thanks in advance

  2. #2
    Zuwayer
    Guest

    check this out

    well junior friend I hope a while loop would work fine for you...
    try something like this...

    ...
    ...
    }while (!eof(fpr));
    where fpr is the file handler opened like
    FILE *fpr= fopen(filename, attributes);

    tell me if it doesn't.. I have been away from C since very long..
    i will double check if it doesn't

    right here at
    [email protected]

  3. #3
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    Firstly... thanks for your solution...

    Secondly... I feel I must apologise...

    I have the most frustrating habit of tackling a problem 'till my hands bleed.... then, in ernest, I post my puzzler to this board... then, through some divine intervention, I figure it out on my own within minutes of returning to it...

    Most upsetting...

    P.S. I used feof instead of eof... are they interchangeable?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I used feof instead of eof... are they interchangeable?
    No.
    EOF is a status return which you get from various file reading functions (like fgetc). feof() is a function.

    If you solved your problem using feof() and not EOF, then you've almost certainly got it wrong. The problem being that feof() will only return true AFTER some other file reading operation (eg fgetc) has returned EOF. If you didn't detect this, then you processed some bogus data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    hmmm... it looks like eof() is also a function looking at Zuwayer's solution... albeit one I can't find anywhere.

    I realise that EOF is a 'result' but I don't have enough experience to say that eof() isn't a function that looks for that result.

    My problem now is that my program works perfectly, despite being wrong :

    This is a small section of the code...

    Code:
    while (i<no_of_words && feof(input) == 0) {
       fgets(temp, 80, input);
       len=strlen(temp);
       list[i]=(char *)malloc(len);
       strcpy(list[i], temp);
       i++;
    }
    So there is a fget* function in there... could that be why it works even though I can't see how feof() would be able to access a result from fgets()?

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Basically the best way I can think of to put this to you is..

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    FILE *fp;
    char ch;
    
    if((fp = fopen("filename.txt", "rt+"))==NULL) {
                printf("Error opening file for mode");
                exit(1);
    }
    
    while((ch =fgetc(fp)) !=EOF)
           putchar(ch);
    
    /*will print file character by character till end of file*/
    
    fclose(fp)
    }
    No doubt if I am wrong well hear about it, LOL...
    Last edited by bigtamscot; 09-20-2001 at 07:46 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fgets(temp, 80, input);
    > len=strlen(temp);
    fgets returns NULL when it hits the end of file, and leaves the buffer unmodified.
    If you look carefully, you may find that the last line in the file is counted twice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. How can i check a directory for certain files?
    By patrioticpar883 in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 05:27 PM
  3. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  4. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  5. prog !feof probs
    By @licomb in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2001, 06:44 AM