I've got a fstream my_file("test.txt"), but I don't know if test.txt exists. In case it exists, I would like to know if I can read it, too. How to do that?
I use Linux.
I've got a fstream my_file("test.txt"), but I don't know if test.txt exists. In case it exists, I would like to know if I can read it, too. How to do that?
I use Linux.
What Operating System/platform?
On Linux/Unix/MacOSX, you can use fstat.
On Windows, you can use GetFileAttributes.
Usually, there is no portable way of doing this with standard C/C++ IO functions.
I would probably go with:
ifstream my_file("test.txt");
if (my_file.good())
{
// read away
}
The good
method checks if the stream is ready to be read from.
You might use Boost.Filesystem. It has a boost::filesystem::exist
function.
I don't know how about checking read access rights. You could look in Boost.Filesystem too. However likely there will be no other (portable) way then try to actually read the file.