?
Solved

C++ Opening a file

Posted on 2006-10-25
3
Medium Priority
?
279 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 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

Industry Leaders: 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 different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

764 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