如何检查一个文件是否存在并且在c++中是可读的?

时间:2022-05-04 23:44:55

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?

我有一个fstream my_file(“test.txt”),但是我不知道是否测试。三种存在。如果它存在,我想知道我是否也能读它。如何做呢?

I use Linux.

我使用Linux。

8 个解决方案

#1


80  

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.

好方法检查流是否已准备好读取。

#2


23  

You might use Boost.Filesystem. It has a boost::filesystem::exist function.

您可能使用Boost.Filesystem。它有一个boost:::文件系统:::exist函数。

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.

我不知道如何检查读访问权限。你可以查看Boost。文件系统。然而,除了尝试实际读取文件之外,可能没有其他(可移植的)方法。

#3


11  

What Operating System/platform?

什么操作系统/平台?

On Linux/Unix/MacOSX, you can use fstat.

在Linux/Unix/MacOSX上,可以使用fstat。

On Windows, you can use GetFileAttributes.

在Windows上,可以使用GetFileAttributes。

Usually, there is no portable way of doing this with standard C/C++ IO functions.

通常,在使用标准的C/ c++ IO函数时没有可移植的方法。

#4


9  

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.

如果您正在使用unix,那么access()可以告诉您它是否可读。然而,如果ACL正在使用中,那么它会变得更复杂,在这种情况下,最好使用ifstream打开文件并尝试读取…如果不能读取,那么ACL可能禁止读取。

#5


3  

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

由于C++11可以使用隐式算子bool代替good():

ifstream my_file("test.txt");
if (my_file) {
  // read away
}

#6


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.

我知道海报最后说他们在使用Linux,但是我有点惊讶,没有人提到Windows的PathFileExists() API调用。

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

您需要包含Shlwapi。lib库,Shlwapi。h头文件。

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

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

函数返回BOOL值,可以这样调用:

if( PathFileExists("C:\\path\\to\\your\\file.ext") )
{
    // do something
}

#7


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.

关于在windows中使用fstat,我不确定这是否是您想要的。从微软开始,文件必须已经打开。统计应该对你有效。

#8


0  

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

C++17,跨平台:用std:::文件系统:::存在,用std:::文件系统::状态& std:文件系统::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.

还要考虑检查文件类型。

#1


80  

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.

好方法检查流是否已准备好读取。

#2


23  

You might use Boost.Filesystem. It has a boost::filesystem::exist function.

您可能使用Boost.Filesystem。它有一个boost:::文件系统:::exist函数。

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.

我不知道如何检查读访问权限。你可以查看Boost。文件系统。然而,除了尝试实际读取文件之外,可能没有其他(可移植的)方法。

#3


11  

What Operating System/platform?

什么操作系统/平台?

On Linux/Unix/MacOSX, you can use fstat.

在Linux/Unix/MacOSX上,可以使用fstat。

On Windows, you can use GetFileAttributes.

在Windows上,可以使用GetFileAttributes。

Usually, there is no portable way of doing this with standard C/C++ IO functions.

通常,在使用标准的C/ c++ IO函数时没有可移植的方法。

#4


9  

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.

如果您正在使用unix,那么access()可以告诉您它是否可读。然而,如果ACL正在使用中,那么它会变得更复杂,在这种情况下,最好使用ifstream打开文件并尝试读取…如果不能读取,那么ACL可能禁止读取。

#5


3  

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

由于C++11可以使用隐式算子bool代替good():

ifstream my_file("test.txt");
if (my_file) {
  // read away
}

#6


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.

我知道海报最后说他们在使用Linux,但是我有点惊讶,没有人提到Windows的PathFileExists() API调用。

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

您需要包含Shlwapi。lib库,Shlwapi。h头文件。

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

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

函数返回BOOL值,可以这样调用:

if( PathFileExists("C:\\path\\to\\your\\file.ext") )
{
    // do something
}

#7


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.

关于在windows中使用fstat,我不确定这是否是您想要的。从微软开始,文件必须已经打开。统计应该对你有效。

#8


0  

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

C++17,跨平台:用std:::文件系统:::存在,用std:::文件系统::状态& std:文件系统::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.

还要考虑检查文件类型。