Linux 如何检查文件是否存在并且在 C++ 中可读?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1383617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a file exists and is readable in C++?
提问by Jerry
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"),但我不知道 test.txt 是否存在。如果它存在,我想知道我是否也可以阅读它。怎么做?
I use Linux.
我使用Linux。
采纳答案by Kim Gr?sman
回答by Pablo Santa Cruz
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 函数执行此操作的可移植方式。
回答by neoneye
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 可能会禁止读取。
回答by Adam Badura
You might use Boost.Filesystem. It has a boost::filesystem::exist
function.
您可以使用Boost.Filesystem。它有一个boost::filesystem::exist
功能。
I don't know how about checking read access rights. You could look in Boost.Filesystemtoo. However likely there will be no other (portable) way than try to actually read the file.
我不知道如何检查读取访问权限。您也可以查看Boost.Filesystem。然而,除了尝试实际读取文件之外,可能没有其他(便携式)方式。
回答by Antoni
回答by Drew Chapin
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,但我有点惊讶没有人提到PathFileExists()
Windows的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
}
回答by Vadzim
Since C++11 it's possible to use implicit operator boolinstead of good()
:
从 C++11 开始,可以使用隐式运算符 bool而不是good()
:
ifstream my_file("test.txt");
if (my_file) {
// read away
}
回答by Roi Danton
C++17, cross-platform: Check file existence with std::filesystem::exists
and readability with std::filesystem::status
& std::filesystem::perms
:
C++17,跨平台:std::filesystem::exists
使用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.
还要考虑检查文件类型。