Last Char of File

This is a discussion on Last Char of File within the C Programming forums, part of the General Programming Boards category; I was just writing a quick command line program and what it does is it reads in a file and ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    157

    Last Char of File

    I was just writing a quick command line program and what it does is it reads in a file and then it takes necessary action to whatever is needed of the file to do.

    But during a loop I have to check and make sure that there isn't the NULL character so I can have the go ahead to keep reading the file. But, if I have a blank file and I test for the NULL character, it's not there. If I FGETS from this blank file the first char isn't NULL. How do I test for the last char of the file?

    Thanks.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>How do I test for the last char of the file?
    If this is a text file then the end of the file is EOF, you can test for it by saying
    Code:
    if (feof(file))
    {
      break;
    }
    Or just use fgets as the loop condition, when it returns null you're either at the end of the file or something blew up and you can use feof() and ferror() to figure out which one after the loop ends
    Code:
    while (fgets(buf, sizeof buf, file) != 0)
    {
      /* Do stuff */
    }
    *Cela*

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>How do I test for the last char of the file?
    It's the last character returned by your read function, before it returns EOF (or whatever signifies EOF).

    A common mistake is to do this:
    Code:
    while (!feof(fp))
    {
       Do stuff
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    i'll just check for the end of file by the NULL char. i wont use the function. but i thought that i did that, and it seemed to look over it. i'll post the code maybe you can see whats wrong.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by stallion
    i'll just check for the end of file by the NULL char. i wont use the function.
    The "End of File" is NOT the NULL marker. The end of file is the EOF marker. It is not a valid char, and cannot be stored in a char. You must use an int.
    Code:
    void dumpfile( FILE *fp )
    {
        int c;
        if( !feof( fp ) )
        {
            while( (c = fgetc( fp )) != EOF )
                putchar( c );
        }
        printf("\nEOF reached.\n");
    }
    Open a file, and pass the file pointer to this function. It will dump the raw contents of the file to the screen one byte at a time. This includes NULL characters (in the case of binary files). NULL is NOT the same thing as the EOF marker.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,657
    Files can have all sorts of odd values within them, and as said before, text files do not have some special character marking the end of contents necessarily. Merely restating what has been said here...just check the ret val of your read function, it will certainly fail when the end of file is reached...what happens, is the FILE structure representing the open stream stores various flags that are 'activated' when you fail on read. Just be sure to check the flags before you close the file, of course, but anyway, feof(FILE*) returns true when end of file has occurred, ferr(FILE*) returns true if an unexpected error occurred.
    clearerr(FILE*) will reset these flags to 'normal'....



    ITSA
    Socket Library!

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    oh okay. i see what you mean now. so i should test the NULL return of fgets to get the end of file. i see now, thanks everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21