xneo27
asked on
C++ Opening a file
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER