Thread: getline() and ifstream

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    45

    getline() and ifstream

    here is the deal, if you press enter, no prob, it waits for a filename, if you enter a bad filename, fin fails and it loops to ask for a valid filename, however, if you then enter a valid filename, it continues to loop regardless.

    Here is the code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	cout << "enter filename: ";
    	string filename;
    	getline(cin, filename);
    	
    	while (filename.empty()) getline(cin, filename);
    	
    	ifstream fin(filename.c_str());
    
    	while (fin.fail())
    	{
    		cout << "ERROR: enter filename: ";
    		getline(cin, filename);
    		
    		while (filename.empty()) getline(cin, filename);
    
    		ifstream fin(filename.c_str());
    	}
    	return 0;
    }
    I think the problem is that fin does not get rediefined, but i am not sure how to get around it. Thanks

    -quizkiwi

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You are declaring two different variables called fin. The first one is in the main scope of the main function. The second one is inside the scope of the while loop. The fin.fail() call is only testing the one created in the outside scope, which doesn't change.

    I'd suggest using the open function instead of trying to use the constructor to open the file. You can pass it the filename just as you do the constructor. Also remember to clear any fail bits if the open fails.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I might as well advertise this website, good for reference
    Have a nice reading. http://www.cplusplus.com/ref/iostream/ios/clear.html
    It's got the answer so it'll be worth it.
    I also see that you redefined "fin" again in the loop, not a good practice

    edit: yup, jlou was just trying elaborate what I said
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    do i have to write something to the file? as in:

    Code:
    myfile << "test";
      if (myfile.fail())
      {
        cout << "Error writing to test.txt\n";
        myfile.clear();
      }
    Will this not overwrite the information on the file?

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You can use is_open instead of fail() or good().

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    okay, i will give that a whirl, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifstream with getline?
    By DigiAcid in forum C++ Programming
    Replies: 13
    Last Post: 03-08-2009, 08:14 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. problem with string class, ifstream, and getline
    By deathbob in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2005, 11:20 AM
  4. ifstream getline
    By jimboob in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2004, 10:37 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM