I need help with a basic operation, but I didn't find an example of it online.
Perhaps someone can point me in the right direction.
The following code should prompt the user for a file's version number.
If the file can be opened it is read, otherwise the user is prompted again.
What is the C++ way of doing this?
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
string fileName;
string ver;
ifstream inf;
do {
cout << "Which version of test? ";
getline(cin, ver);
fileName = "test" + ver + ".txt";
inf = fileName.c_str(); // failed attempt to assign a file name to an ifstream object
if (inf)
{
break;
}
cerr << "File could not be opened for reading!" << endl;
} while(true);
// inf is used here
}