Thread: C++ Without Fear Example Won't Work

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    C++ Without Fear Example Won't Work

    Hi,

    I'm working my way through the book C++ Without Fear, after reading the recommendation for it on this site. However, one of the programs doesn't work. Basically it prompts the user to enter a filename, then it creates a file input stream. It then checks to see if that file input stream has a NULL value before continuing. However, for some reason, the check always comes back negative.

    Can anyone help me out? Here is the code. Running the program should make it pretty obvious where the problem is because it seems like it's probably something simple. Thanks guys.

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        int c;   // input character
        int i;   // loop counter
        char filename[81];
        char input_line[81];
    
        cout << "Enter a file name and press ENTER: ";
        cin.getline(filename, 80);
    
        ifstream file_in(filename);
    
        if (! file_in) {
            cout << "File " << filename << " could not be opened.";
            return -1;
        }
    
        while (1) {
            for (i = 1; i <= 24 && ! file_in.eof(); i++) {
                file_in.getline(input_line, 80);
                cout << input_line << endl;
            }
            if (file_in.eof())
                break;
            cout << "More? (Press 'Q' and ENTER to quit.)";
            cin.getline(input_line, 80);
            c = input_line[0];
            if (c == 'Q' || c == 'q')
                break;
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I would transform if(!file_in) to if(!file_is.is_open()).

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Oh dear I just realised what the problem was. I'd been rushing through the book, and didn't realise it would only open files that are already there. Oops sorry guys.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Quote Originally Posted by Desolation View Post
    I would transform if(!file_in) to if(!file_is.is_open()).
    BTW what's better about this? Thanks.

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    There is a typo in it, but instead of just validating the pointer, it validates that the file is in fact open.
    Code:
    if(!file_in.is_open())

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Code:
    if(!file_in)
    This checks if any of the failure status bits are set.

    Code:
    if(!file_in.is_open())
    This checks whether the file is open.

    A file can be open, and yet have failure bits set. Similarly, the stream can have failure bits set without the file being open (the fact that the file isn't open might be the CAUSE of the failure).

    So the two things don't do the same thing at all. However, I'd use the first form, not the second, because having the file open doesn't matter if the stream is in an error state...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by brewbuck View Post
    Code:
    if(!file_in)
    This checks if any of the failure status bits are set.

    Code:
    if(!file_in.is_open())
    This checks whether the file is open.

    A file can be open, and yet have failure bits set. Similarly, the stream can have failure bits set without the file being open (the fact that the file isn't open might be the CAUSE of the failure).

    So the two things don't do the same thing at all. However, I'd use the first form, not the second, because having the file open doesn't matter if the stream is in an error state...
    But he wants to check wether or not the file is open.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM