Opening a file using a String

This is a discussion on Opening a file using a String within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a loop that repeats untill I get a valid file name from the user. Unfortunatly ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    36

    Opening a file using a String

    I am trying to make a loop that repeats untill I get a valid file name from the user. Unfortunatly it only works if the correct file name is input on the first attempt. Can someone give me some ideas in the right direction on what I am doing wrong?


    Code:
            cout << "Please enter the name of the file to open: ";
    
    	cin >> fileName;
    
    	cout << "You entered:" << fileName << endl;
    
    	inFile.open(fileName.c_str());
    
    	while (!inFile)
    	{
    		cout << "Please enter the name of the file to open: ";
    
    		cin >> fileName;
    
    		cout << "You entered:" << fileName << endl;
    
    		inFile.open(fileName.c_str());
    	}

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    What types of objects are inFile and fileName?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    How about something like this?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using std::cout; 
    using std::cin; 
    using std::endl;
    using std::ifstream;
    using std::string;
    
    int main(void)
    {
       string fileName;
       ifstream inFile;
       do {
          cout << "Please enter the name of the file to open: ";
          cin >> fileName;
          cout << "You entered:" << fileName << endl;
          inFile.open(fileName.c_str());
       } while ( !inFile );
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    maxhavoc, here are the declarations

    Code:
    ifstream inFile;
    
    string fileName;
    Thanks for the help guys I think a do while would work properly but I cant figure out why the only the priming read works in a regular while loop.

    I am not sure what "clear" is refering too if that is a hint...

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    Code:
    	
            ifstream inFile;
    
    	string fileName;
    
    	cout << "Please enter the name of the file to open: ";
    
    	cin >> fileName;
    
    	cout << "You entered:" << fileName << endl;
    
    	inFile.open(fileName.c_str());
    
    	while (!inFile)
    	{
    		inFile.clear();
    
    		cout << "Please enter the name of the file to open: ";
    
    		cin >> fileName;
    
    		cout << "You entered:" << fileName << endl;
    
    		inFile.open(fileName.c_str());
    	}
    Okay thanks everyone inserting inFile.clear seems to have fixed the problem. Can anyone tell me what it does? Thanks again!

    JL

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    It resets all of the fail bits in the stream object.

    ios::fail()
    ios::bad()
    ios::eof()
    Last edited by SlyMaelstrom; 06-15-2006 at 02:14 PM.
    Sent from my iPadŽ

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    It clears any error flags on that object. Without that, the error flags that do happen to be set will prevent any action on that object (like trying to open the file a second time).
    "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

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    I see...

    Thanks again for the help everyone! I was stuck for a while on that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

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