User selection of text files

Sep 19, 2012 at 12:33am
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 
Sep 19, 2012 at 1:01am
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);

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