Thread: Annoying getline does not work

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    Annoying getline does not work

    This is an extract from a much bigger program:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	ifstream input;
    	bool invalid;
    	string message, infile;
    
    	invalid = false;
    	do
    	{	
    		if(invalid == true)
    		{
    			cout << "The " << infile << " does not exist. Please specify another file name." << endl;
    		}
    		invalid = true;
    		cout << "What is the name of the file you want to read from?" << endl;
    		cin >> infile;
    		input.open(infile.c_str());
    	}while(input.is_open() == false);
    
    	while(input.eof() == false)
    	{
    		getline(input, message);
                    //do crazy irrelevant stuff with 'message' string
    	}
    }
    What I want to do, is to ask user for a file name which they want to read all the lines from, and do some operations at one of the lines at a time. Also, if the user specifies a file name that does not exist, I want the program to ask for the file name again.

    In my program, when the user specifies a correct file name at the first time, everything works. However, if they input a wrong name at least once and eventually a correct one, the getline() function does not read anything!! It just returns an empty string into the 'message' and additionally, the last while loop is infinite.
    What the hell? How can I make it work?

    Edit:
    I am using Visual Studio 2008. My friend with code::blocks compiled the code above and it works for him. Can it be a compiler bug? How to fix it then?
    Last edited by kulfon; 03-01-2011 at 03:56 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    fstream - C++ Reference
    If you fail to open the file, various error states are set.

    Use the clear() method to make sure the fstream is in a consistent state before opening the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Instead of this:
    Code:
    while(input.eof() == false)
    {
        getline(input, message);
        //do crazy irrelevant stuff with 'message' string
    }
    do this:
    Code:
    while (getline(input, message) { ... }
    Of course, after you make sure the file actually was opened. E.g. with file.is_open()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why first getline doesnt work?
    By kulfon in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2011, 10:17 AM
  2. Help with getline() and file reading
    By evilkillerfiggi in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2005, 03:49 PM
  3. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  4. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  5. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM