Solved

C++ Opening a file

Posted on 2006-10-25
3
274 Views
Last Modified: 2012-05-05
Okay so heres my function I want it to open a file but if the file doesnt open i want it to do the else statement then start the function over again.
What would be the best method for doing so?
Im also open to critisism of currently existing code.


void OpenFile()
{
      
      cout << "Enter in the name of the input file (ctrl-c to exit):";
      cin >> fileName;
      cout << fileName << endl;
        string line;
        ifstream myfile (fileName.c_str);
        if (myfile.is_open())
        {
            cout << "Input file " << fileName << " was successfully opened!" << endl << endl;
        }
      
        else
        {
        cout << "Error opening input file named: " << fileName << endl;
        cout << "Please try again." << endl << endl;
        myfile.close();
        myfile.clear();
 
} //end OpenFile
0
Comment
Question by:xneo27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 500 total points
ID: 17807709
Just put it in a loop. Something like:

void OpenFile()
{
     bool fileopen=false;
    while(!fileopen)
    {    cout << "Enter in the name of the input file (ctrl-c to exit):";
         cin >> fileName;
         cout << fileName << endl;
         string line;
         ifstream myfile (fileName.c_str);
         if (myfile.is_open())
         {
            cout << "Input file " << fileName << " was successfully opened!" << endl << endl;
            fileopen=true;
         }
     
         else
        {
            cout << "Error opening input file named: " << fileName << endl;
            cout << "Please try again." << endl << endl;
            myfile.close();
           myfile.clear();
         }
    }
 
} //end OpenFile
0
 

Author Comment

by:xneo27
ID: 17812523
Thank you very much, is there a way to make the ifstream myfile accessable from outside this function?
0
 
LVL 16

Expert Comment

by:imladris
ID: 17816212
You should be able to return it to the caller. The issue that then needs to be deal with is the declaration of myfile. As it stands, it is declared/created *inside* OpenFile. So it exists on the stack, which allows for the creation of "local" variables (and argument passing) and, more importantly, destroys them when the function returns (by popping everything back off the stack).
So, to make that work, you would have to pass something *in* to OpenFile; and not a regular pass in (which is by value, i.e. a copy is created and put on the stack for the called function) because that will likewise disappear when the function returns. You would need a pointer or a reference. Since this is C++ the more elegant solution of pass by reference would seem to be the right tactic. Something like:

void OpenFile(ifstream &myfile)
{
     bool fileopen=false;
    while(!fileopen)
    {    cout << "Enter in the name of the input file (ctrl-c to exit):";
         cin >> fileName;
         cout << fileName << endl;
         string line;
         myfile (fileName.c_str);
         if (myfile.is_open())
         {
            cout << "Input file " << fileName << " was successfully opened!" << endl << endl;
            fileopen=true;
         }
     
         else
        {
            cout << "Error opening input file named: " << fileName << endl;
            cout << "Please try again." << endl << endl;
            myfile.close();
           myfile.clear();
         }
    }
    return;
} //end OpenFile
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Starting up a Project

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question