files & streams

This is a discussion on files & streams within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by gunghomiller Code: char ch=cin.get();; ... while (ch != EOF) a char cannot hold an EOF value. You'll ...

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by gunghomiller View Post
    Code:
        char ch=cin.get();;
    ...    
        while (ch != EOF)
    a char cannot hold an EOF value. You'll need an int for that.

    Also, you use the ch value before you check to see if it's not EOF.

    also, try turning your warning levels up. You have a useless semicolon there.

  2. #17
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by robwhit View Post
    a char cannot hold an EOF value. You'll need an int for that.
    As far as i'm aware, EOF is #define'd as -1 - so a char is perfectly well equipped to hold it.

    That matter aside, reading from a file while-not-EOF is the wrong idiom, the correct way is to read while there is data to read. in other words,
    Code:
    std::ifstream infile("test.txt");
    char ch;
    while( infile >> ch )
    {
        // do something with ch
    }
    This ensures that the state of the ifstream is checked at the same time as reading. if anything goes wrong (like attempting to read past the end of the file), the loop stops before anything bad can happen.

  3. #18
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    As far as i'm aware, EOF is #define'd as -1 - so a char is perfectly well equipped to hold it.
    Char may be unsigned and "equipped" to hold values from 0 to 255...
    When you store the EOF in char - you get something like 255 - which is fully legal value for the char...
    When you get the file containing this value - you will stop reading it on this char just because you do not want to hear when people knowing something better telling you how you should do this or that...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. streams and files
    By chad101 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 11:54 AM
  5. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 02:56 PM

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