File Streams Question

I have been looking for this specific method of reading from files, but I could not seem to find anything useful.
What I'm actually trying to do is reading the given file path as a variable.
Example:

1
2
3
4
5
6
7
8
// code
string path;
cin >> path;
ifstream in(path);
while (in && !in.eof() && in.is_open()){
     // code
}
//code 


I don't know if that is actually possible, but I'm trying to give the user the possibility of choosing which file to read from, without the need of it to be positioned in the same folder, and without the need of editing the C++ code in order to get it working (or rename the files to those name).

Any help in this way is highly appreciated. Thanks.

PS: Or, if possible, the user to enter the file name, and the C++ program is set to look-up the whole hard drive for the specific file, and if found, open it. I know that might take a lot of time, but I'm interested in knowing if that is possible. Thanks again.
Last edited on
Don't check a stream is it's open or reached EOF, just check if it's good.
1
2
3
4
5
6
7
string path;
cin >> path;
ifstream in(path.c_str());
while (in && !in.eof() && in.is_open())
{
     // code
}
Last edited on
if possible, the user to enter the file name, and the C++ program is set to look-up the whole hard drive for the specific file, and if found, open it

It is possible with boost.filesystem library (soon to be part of standard C++, and already shipped as part of C++ by Visual Studio)

http://www.boost.org/doc/libs/release/libs/filesystem/doc/index.htm
Thank you guys. Now I can start working on my code. Topic closed. :)
Topic archived. No new replies allowed.