ifstream assignment

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?

My failed attempt is below. Apparently there is no way to assign a new file name to an ifstream object.
http://www.cplusplus.com/reference/fstream/ifstream/ifstream/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <string>
using namespace 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
}
you should swap inf = fileName.c_str(); with inf.open(fileName.c_str());
Thanks adriano782, that worked!
Topic archived. No new replies allowed.