User selection of text files

Does anyone know how to open a text file such that the user selects the name of the text file?

i.e.
1
2
3
4
5
infile ifstream;
string name;
cin>>name;

ifstream.open(name.txt); //Name is the user defined string 
except that it would just be the variable 'name'. This means that you would need to append the file type to the end of the string before passing it as a parameter to the ifstream.open() method.

the string class will do this for you easily.

1
2
3
4
5
6
ifstream inFile; //notice here that the identifier and keyword have exchanged places.
string name;
cin>>name;

name+=".txt";
ifstream.open(name);

Line 6 in popgrady's example should be inFile.open( name.c_str() );, assuming string is an std::string.
Last edited on
Thanks so much! This really helps me out.
this information is too much good for me for understanding...
Topic archived. No new replies allowed.