Checking file exists through fstream

Was doing some more file stuff, and before I go too deep into all the stuff I'm planning...

Is this method of checking for a file's existence platform independent?
Are there any side effects that could arise from this?

1
2
3
4
5
6
7
8
#include <fstream>
bool checkExistence(std::string filename)
{
    fstream f;
    f.open(filename);

    return f.is_open();
}
Last edited on
Are there any side effects that could arise from this?
Yes. fstream opens for reading and writing by default, creating new file if needed.
creating new file if needed

Are you sure about that? It only creates a new file if I do ofstream f;, like what it shows in the tutorial.
If I do fstream f;, it has never created a file through open unless I do f.open(filename, ios::out|ios::in|ios::trunc); (see what I was doing in http://www.cplusplus.com/forum/beginner/136617/ )

Anyway, so to be safe I should open it as an ifstream?
1
2
3
4
5
6
7
8
#include <fstream>
bool checkExistence(std::string filename)
{
    ifstream f;
    f.open(filename);

    return f.is_open();
}
...and THIS should be a platform independent way to check, with no side-effects?
Last edited on
If I do fstream f;, it has never created a file through open
It can behave differently on different platforms. And it is better to not rely on something so vague like OS internal working.

THIS should be a platform independent way to check, with no side-effects?
Nope, there is stil plenty of ways how this can fail:
1) File exists but opened in another process in monopoly mode.
2) File exists, but security permissions does not allow you to open it (apparently there is no a single file in Windows directory for example)
3) System is heavily loaded and couldn't open file for you.
4) You have run out of file descriptors.

Platform independed way will be using boost::filesystem or waiting for filesystem TR to be included in next language standard version.
Ah okay, I'll be sure to use ifstream for things like that. Might look into the boost stuff if I make a serious project, although for now it seems to be okay enough. Thanks for the info!
It can behave differently on different platforms.

No the C++ standard states the default open modes and by default an fstream will not create a file if it doesn't exist (see section 27.9.1.17 of the last working draft of the current standard (N3374 2012-02-28)).

Remember the default open mode for fstream is in | out which is the same as the C-stdio open mode "r+" (see Table 132 of the current standard) which is:
read/update: Open a file for update (both for input and output). The file must exist.


When using the default open modes only an ofstream will create a file if the file doesn't exist.
Last edited on
@jib thanks for correcting me.

I remembered some other funny things which can happen to files:
1) Unnamed file which cannot be accessed in filesystem by anything aside from descriptors alredy linked to it (tmpfile() on Linux)
2) Filesystem where any possible file can be opened and opening cannot fail (bachelor thesis in our uni).
3) Named files which exist only in filesystem driver journal (similar to RAM disk) (same thesis)
Topic archived. No new replies allowed.