I'm not exactly sure what OP is asking, tbh. You can't reuse the variable called "name", because C++ is statically typed, meaning type cannot change at run-time. But it doesn't make sense to call a file stream "name" in the first place...
But also note that before C++11, std::fstreams had to take in const char*'s (c strings) and couldn't take in std::strings as a argument. If you compile with C++11 or later, which you should, you can then compile by directly passing the string. Much cleaner.
modern C++11/14/17:
1 2 3 4 5 6
#include <string>
#include <fstream>
int main () {
std::string name = "myfile";
std::fstream file(name);
}
old C++ (pre C++11):
1 2 3 4 5 6
#include <string>
#include <fstream>
int main () {
std::string name = "myfile";
std::fstream file(name.c_str());
}
this looks like a twist on the "enum problem" to me.
that is, you can make an enum:
enum e
{
zero, one, two, three
};
but there is no simple way to print "zero" for zero etc and no simple way to have a user type "zero" and get back to the zero value.
or more generally, it is nontrivial to connect the a string version of a variable name to the variable. It can be done with a vector of strings and a class with some smoke and mirrors but the language proper does not support doing this kind of thing directly.