In my program written in C++, I open a file as follows:
std::ifstream file("testfile.txt");
This usage can handle the scenario where the input file has a fixed name of “testfile.txt”.
I would like to know how to allow the user to input a file name, e.g., “userA.txt”, and the program automatically opens this file, “userA.txt”.
Use a variable. I suggest finding a good introductory book if you’re not clear on what they are yet.
If the filename can have spaces in it, then
cin >>(which stops input at the first space as well as newline) won’t cut it. Instead, you can usegetline():