66

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.

Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
Jerry
  • 1,139
  • 3
  • 9
  • 19

8 Answers8

88

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.

Kim Gräsman
  • 7,438
  • 1
  • 28
  • 41
  • 17
    This way you not only check if it exists & is readable, you actually open it. – xtofl Sep 05 '09 at 17:08
  • 1
    Yeah, that's true. I read the OP's question that the file was already opened anyway, but I could be wrong. – Kim Gräsman Sep 05 '09 at 19:45
  • 1
    Moreover, it cannot tell whether the file couldn't be opened because it didn't exist or because of access permission problems. – SasQ Feb 16 '19 at 11:44
22

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 than try to actually read the file.

EDIT (2021-08-26): C++17 introduced <filesystem> and there you have std::filesystem::exists. Boost is no longer needed for this.

Adam Badura
  • 5,069
  • 1
  • 35
  • 70
  • Does boost filesystem handle very long paths on windows (> 256)? We recently ran into the problem that the non unicode windows API has a maximum of 256 characters. – Richard Corden Sep 07 '09 at 10:17
  • I don't know. You might check in documentation (at the link in my answer) but I'm not sure whether you will find it and its long. You might also ask on Boost news group. Another way is to simply check it. – Adam Badura Sep 07 '09 at 15:11
  • @Richard Corden I checked very long paths with function boost::filesystem::create_directories. ANSI version (for char) threw an exception if given to long path (but the limit was somehow reached around 250 rather then MAX_PATH which is equal to 260). UNICODE version (for wchar_t) created as much folders as ANSI version would without throwing and returned successfully so not only it did not do the job but also it did not report the error. I don't know how is it with other functions but I suspect now they might behave badly as well. – Adam Badura Sep 08 '09 at 06:49
12

C++17, cross-platform: Check file existence with std::filesystem::exists and readability with std::filesystem::status & std::filesystem::perms:

#include <iostream>
#include <filesystem> // C++17
namespace fs = std::filesystem;

/*! \return True if owner, group and others have read permission,
            i.e. at least 0444.
*/
bool IsReadable(const fs::path& p)
{
    std::error_code ec; // For noexcept overload usage.
    auto perms = fs::status(p, ec).permissions();
    if ((perms & fs::perms::owner_read) != fs::perms::none &&
        (perms & fs::perms::group_read) != fs::perms::none &&
        (perms & fs::perms::others_read) != fs::perms::none
        )
    {
        return true;
    }
    return false;
}

int main()
{
    fs::path filePath("path/to/test.txt");
    std::error_code ec; // For noexcept overload usage.
    if (fs::exists(filePath, ec) && !ec)
    {
        if (IsReadable(filePath))
        {
            std::cout << filePath << " exists and is readable.";
        }
    }
}

Consider also checking for the file type.

Roi Danton
  • 7,933
  • 6
  • 68
  • 80
  • 7
    Great answer, but quick question: How can we check if *this executable* has permissions to read the file, rather than *who has permissions to read the file*? – user14717 Oct 16 '19 at 13:38
  • You mention this is cross-platform. How are things like owner_read/grou_read and others_read resolved in Windows, where this isn't how permissions are structured? – Tyler Shellberg Aug 08 '22 at 17:04
11

if you are on unix then access() can tell you if it's readable. However if ACL's are in use, then it gets more complicated, in this case it's best to just open the file with ifstream and try read.. if you cannot read then the ACL may prohibit reading.

neoneye
  • 50,398
  • 25
  • 166
  • 151
10

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.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • 1
    Why do you say that, you could always just try to open a file with fopen and if it returns 0 you can deduce portably that the file is nonexistent. – Blindy Sep 05 '09 at 15:48
  • 1
    fstat is available on windows, too, in `sys/stat.h`. – xtofl Sep 05 '09 at 17:11
  • 1
    @Blindy or it does exist, but you don't have permissions to access it. What then? :q Your deduction is wrong. – SasQ Feb 16 '19 at 11:46
  • @sasq makes no difference, the title literally says "and is readable". A file you don't have permission to access is by definition non-readable. Critical thinking will get you far in life! – Blindy Feb 18 '19 at 05:22
  • 1
    @Blindy I was refering to your comment, not to the title or original post. My point was that opening a file just to check if it exists may have side effects that one does not necessarily desire, including a possibility to accidentally damage the file (e.g. if one opens it for writing as well and it happens that it exists). And spare me the "critical thinking" ad personam. – SasQ Feb 18 '19 at 06:01
  • Why would you open it for writing if you just tested for its existence and the function returned false? You're running in circles trying to find weird edge cases when in fact this is perfectly fine as it is. You must be an inexperienced student. – Blindy Feb 20 '19 at 20:10
6

Since C++11 it's possible to use implicit operator bool instead of good():

ifstream my_file("test.txt");
if (my_file) {
  // read away
}
Vadzim
  • 24,954
  • 11
  • 143
  • 151
2

I know the poster eventually said they were using Linux, but I'm kind of surprised that no one mentioned the PathFileExists() API call for Windows.

You will need to include the Shlwapi.lib library, and Shlwapi.h header file.

#pragma comment(lib, "shlwapi.lib")
#include <shlwapi.h>

the function returns a BOOL value and can be called like so:

if( PathFileExists("C:\\path\\to\\your\\file.ext") )
{
    // do something
}
Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
0

Concerning the use of fstat in windows, I am not sure if it is what you want. From Microsoft the file must be already open. Stat should work for you.

Antoni
  • 2,542
  • 20
  • 21