?
Solved

C++ Opening a file

Posted on 2006-10-25
3
Medium Priority
?
290 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
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 2000 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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression

862 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