EOF not found

This is a discussion on EOF not found within the C++ Programming forums, part of the General Programming Boards category; Hi, I wrote a simple program based on the file io tutorial. It 's supposed to echo a file but ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    2

    EOF not found

    Hi, I wrote a simple program based on the file io tutorial. It 's supposed to echo a file but unfortunatel it dosent seam to hit the EOF?

    Code:
    int main(void){
    
    ifstream in("my_file");
    
    char input;
    
    in.get(input);
    while(input != EOF){
    cout << input;
    in.get(input);
    }
    
    in.close();
    
    return 1;
    }
    Any help is appreciated.
    Last edited by bif22; 09-29-2002 at 08:19 AM.

  2. #2
    Registered User Unimatrix139's Avatar
    Join Date
    Jun 2002
    Posts
    55
    Try replacing EOF with NULL - that always works for me
    Kree'ta Tau'ri! Chaapa'ai!

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Did you include :

    Code:
     
    
    #include <fstream>
    and

    where is your open function

    Code:
     
      ifstream in;
      in.open("myfile");
    Mr. C

  4. #4
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Also, you could say something like:

    Code:
    while (!in.eof())
     {
    
    
      }
    Mr. C

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    2
    Hi,

    thanks for quick replys,

    replacing with !in.eof() worked!

    I dont understand though why

    input != EOF

    didnt work though? (That sort of thing works in C?)

    Cheers again

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    You can still test for EOF in C++, the way I showed is preferred but:

    You can try:

    Code:
     
    while ((input = cin.get()) != EOF)
    
    {
    
    }
    mr. C

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >where is your open function
    Right here:
    Code:
    ifstream in("my_file");
    >the way I showed is preferred
    Is it really? What about that annoying problem of how the eof flag is not set until after the loop body has begun execution? A better way would be like so:
    Code:
    while ( in.good() ) {
      // Work with in
    }
    // If you feel so inclined to make sure nothing went wrong:
    if ( in.fail() ) {
      // Handle the error.
    }
    -Prelude
    My best code is written with the delete key.

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    I believe it is the preferred way. In all the reference and text books I have seen (and I use) - I see them use the way I showed. Where is book or reference that uses good or fail with text files?

    mr. C.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >In all the reference and text books I have seen
    Books are very often wrong. If it works for you then I won't stop you from using it, just be aware that there are problems with your preference, so teaching it to others while explaining it to be the best option is not the best idea.

    >Where is book or reference that uses good or fail with text files?
    All I have to go on (that I trust) is the C++ Standard dictating that the eof flag is not set until after you try to read past EOF, and the recommendations of many of the best C++ programmers in the world. Also that thing they call experience, I know that your method has problems because I've seen it happen on a regular basis. I'd be glad to quote the standard later if you like, I don't have it with me at the moment.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Prelude needs no corroboration from me, but
    Code:
    while (!in.eof())
       {
    
       }
    also tests only for the EOF flag and ignores flags, such as 'badbit' and 'failbit', that may be set during the read. The code she suggests accounts for such a possibility and, therefore, should be considered the "preferred" method.

    P.S. For what it's worth, this same information is in an old C++ text of mine. (Don't ask Prelude for the "standard", btw. If she says it's in there, it is.)

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and to add a bit more (I don't think this is already said):
    Code:
    char input;
    while (input != EOF)
    ...
    This will never work properly, as EOF is an int and input is a char which will never be able to be equal to the value of EOF.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    it's generally not a good idea to mix C input/output with C++ input/output. EOF is a special integer value, bigger than a byte, which is sent by the operating system when a FILE* has reached end of file. c++ may not send that at all. (it still might... but as a programmer you're aiming for portability and standards compliance.)

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Code:
    while ( in )
       {
    
       }
    The return value is non-zero as long as the 'read' is valid. If the 'read' becomes invalid, the value returned is zero and the loop is terminated. (Just an additional piece of info here.)

    I digress to Prelude's code. Assuming that you want to handle an error appropriately, i.e not let the program simply "crash", you'll want to add some error-handling properties to your code.

    P.S. Thanks to both Hammer and ygfperson for their input. Much appreciated...by me, if no one else.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Symbol file not found for *.ko
    By yanglei_fage in forum Linux Programming
    Replies: 3
    Last Post: 03-30-2009, 08:48 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. doesn't stop before EOF
    By zmaker5 in forum C Programming
    Replies: 15
    Last Post: 08-03-2007, 02:25 PM
  4. Check for EOF when using fgets
    By daghenningsorbo in forum C Programming
    Replies: 6
    Last Post: 05-16-2007, 06:48 PM
  5. fscanf and EOF
    By .ZG. in forum C Programming
    Replies: 3
    Last Post: 05-30-2004, 07:49 AM

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