Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ Opening a file

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Six Sigma Control Plans
Introduction to Processes

618 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