Thread: Problem with reading a file

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Problem with reading a file

    I am using the following code:

    (IN MAIN)
    Code:
    do {
    	fileOpen = OpenInputFile(infile, inputpath);
    } while (!fileOpen);
    
    
    (OUT OF MAIN)
    bool OpenInputFile(ifstream& infile, string& inputpath)
    {
    	// prompt user for input file names
    	cout << "Input the name of the input file" << endl;
    	cin >> inputpath;
    	
    	//open file and verify its presence
    	infile.open(inputpath.c_str());
    
    	if (infile.fail())
    	{
    		cout << "Sorry, the file '" << inputpath << "' was not found." << endl;
    		cout << "Input another path" << endl;
    		return false;
    	}
    		
    	// notify user if opening was a success
    	cout << "Input file '" << inputpath << "' is open for reading" << endl;
    	return true;
    }
    When I enter the file name correctly the FIRST TIME it works fine, but I want the users to be able to make a mistake and type in another path, However, if you type in a wrong file path the first time, no matter what, it will always come back false after that, no matter how many times you try. Please Help.

    PS don't laugh at my failuresome attempt at writing code.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your file stream variable goes into an error state which will persist until you clear it... which you should do before the next attempt at opening the file.

    Code:
    bool OpenInputFile(ifstream& infile, string& inputpath)
    {
        // prompt user for input file names
        cout << "Input the name of the input file" << endl;
        cin >> inputpath;
    	
        //open file and verify its presence
        infile.open(inputpath.c_str());
    
        if (infile.fail())
        {
            cout << "Sorry, the file '" << inputpath << "' was not found." << endl;
            cout << "Input another path" << endl;
            infile.clear();
            return false;
        }
    		
        // notify user if opening was a success
        cout << "Input file '" << inputpath << "' is open for reading" << endl;
        return true;
    }
    "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

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Thanks

    That did it, thanks so much.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    this works too

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    bool OpenInputFile(ifstream& infile, string& inputpath)
    {
          // prompt user for input file names
          cout << "Input the name of the input file" << endl;
          cin >> inputpath;
    
          //open file and verify its presence
          infile.open(inputpath.c_str(), ios::in);
    
          if(!infile.is_open())
         {
            cout << "Sorry, the file '" << inputpath << "' was not found." << endl;
            cout << "Input another path" << endl;
            return false;
          }
    
          cout << "File '" << inputpath << "' is open for reading.\n";
          return true;
    }
    
    int main(void)
    {
      string inputpath;
      ifstream infile;
      bool fileOpen;
    
      do {
        fileOpen = OpenInputFile(infile, inputpath);
      } while (!fileOpen);
    
    
      return 0;
    }

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. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM