Solved

C++ Opening a file

Posted on 2006-10-25
3
233 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCount chalenge 3 50
firstChar challenge 13 82
wordmultiple challenge 12 89
Counting documents in a Domino View 3 31
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now