Annoying getline does not work

This is a discussion on Annoying getline does not work within the C++ Programming forums, part of the General Programming Boards category; This is an extract from a much bigger program: Code: #include <fstream> #include <iostream> #include <string> using namespace std; int ...

  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 02:56 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    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, 09: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, 10:46 PM

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