Thread: ifstream problem

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    37

    ifstream problem

    Here is the code :

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main (int argc, char * const argv[]) {
        // insert code here...
        int c;	//input character
    	int i;	//loop counter
    	string filename;
    	string input_line;
    	
    	cout << "Enter a file name and press ENTER: ";
    	getline(cin, filename);
    	
    	ifstream file_in(filename.c_str());
    	
    	if (!file_in)
    	{
    		cout << "File " << filename;
    		cout << " could not be opened.";
    		return -1;
    	}
    	
    	
    	while (1)
    	{
    		for (i = 1; i <= 24 && !file_in.eof(); i++) {
    			getline(file_in, input_line);
    			cout << input_line << endl;
    		}
    		
    		if (file_in.eof())
    			break;
    		
    		cout << "More? (Press 'Q' and ENTER to quit.)";
    		getline(cin, input_line);
    		
    		c = input_line[0];
    		if (c == 'Q' || c == 'q')
    			break;
    	}//end while
    	
    	return 0;
    }
    When I run the previous code I get an "File myfile.txt could not be opened"
    Why does the program executes this !file_in.
    I believe that file_in is not null.
    thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by skiabox
    When I run the previous code I get an "File myfile.txt could not be opened"
    Why does the program executes this !file_in.
    I believe that file_in is not null.
    Your own error message gives you a starting point: the file could not be opened. Perhaps it does not exist. Perhaps there is a permission problem. In this case the concept is not that of "null" or "not null", but of whether there is an error with the stream or if the stream is in a good state. Clearly, the stream is not in a good state, so if you believe otherwise, then you are just deluding yourself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    ok I managed to read the file in the debugger console, but I don't get the 24 lines pause.
    Any ideas?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    for (i = 1; i <= 24 && !file_in.eof(); i++) {
        getline(file_in, input_line);
        cout << input_line << endl;
    }
    This is flawed due to how the end-of-file flag gets set - it does not get set until after an attempt to read past the physical end of file occurs. Let's assume in the last iteration of the loop body you successfully read the last line in the file. The eof test will still return false at this point, incorrectly indicating that there is more to be read. Therefore you'll reenter the loop body one more time even though you're already at the end of the file. The getline call will fail (testing eof at this point would return true) causing the buffer input_line to remain unchanged with the previous iteration's contents. Because you're already in the body of the loop, the way you've coded things means it's already too late to avoid the output statement. This would then likely result in the same last line being displayed to the screen a second time.

    The better way to do this is to test the return result of the read statement directly (the getline call). The getline call itself will return a reference to the stream (the file) it's reading from. In a true/false context this reference can be examined to determine if the read was successful (more to be read) or not (no more data to be read or some other error has occurred). This would look like:
    Code:
    for (i = 1; i <= 24 && getline(file_in, input_line); i++) {
        cout << input_line << endl;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Perhaps the file reached EOF.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    37
    In fact my program is ok.
    The problem was the text file that was large but with only 5 or 6 \n characters. (I can't express it in another way- it is when you press enter when you write the text file)
    I've added some more phrases, each one in its own line and it worked fine!
    Thank you for your answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  2. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM