How to allow user specify file path for fstream?

Hi, I'm very new to c++ but I'm trying to write a program which displays data from a file.

However, i don't know how to allow the user to specify the file path, only how to specify it myself in code.

ifstream mlb; //create an in file stream
mlb.open("path"); //replace path

i want the user to enter in the file path for this file, any help is greatly appreciated!
i think if not wrong just do this
mlb.open(path.txt);
and the file you creat should bee in the same dirctarey.
closed account (D80DSL3A)
This simple method works:
1
2
3
4
char fName[80];
cout << "Enter filename: ";
cin >> fName;
ifstream mlb(fName);// opens the file - if it exists 
Awesome, thank you very much fun2code!

I tried that solution first, but with the path as a string instead of a char.

Thanks!
closed account (D80DSL3A)
You're welcome. It will work with a string too if you call c_str() on it:
1
2
3
4
string fName;
cout << "Enter filename: ";
cin >> fName;
ifstream mlb( fName.c_str() );
Last edited on
Topic archived. No new replies allowed.