如何检查 C++ 中是否存在文件对于 Windows 程序?
这是仅适用于 Windows 的程序,因此可移植代码不是问题。
我只需要:
bool DoesFileExist( LPWSTR lpszFilename )
{
// ...
}
This is for a Windows-only program so portable code is not an issue.
I need simply:
bool DoesFileExist( LPWSTR lpszFilename )
{
// ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
根据可敬的 Raymond Chen 的说法,如果您迷信,您应该使用 GetFileAttributes< /a>.
According to the venerable Raymond Chen, you should use GetFileAttributes if you're superstitious.
最好的方法是什么在C++中检查文件是否存在? (跨平台)
使用
_access
或stat
。What’s the best way to check if a file exists in C++? (cross platform)
Use
_access
orstat
.这是一个有点复杂的问题。没有 100% 的方法来检查文件是否存在。您所能检查的只是“我可以某种程度访问的文件是否存在”。对于非超级用户帐户,很可能存在您无权访问的文件,访问检查不会显示文件的存在情况。
例如。可能无法访问特定目录。那么无法确定该目录中是否存在文件。
话虽这么说,如果您想检查文件是否存在,您可以使用以下之一进行访问:_stat、_stat64、_stati64、_wstat、_wstat64、_wstati64
This is a bit more of a complex question. There is no 100% way to check for existence of a file. All you can check is really "exstistence of a file that I have some measure of access to." With a non-super user account, it's very possible for a file to exist that you have no access to in such a way that access checks will not reveal the existincae of an file.
For instance. It's possible to not have access to a particular directory. There is no way then to determine the existence of a file within that directory.
That being said, if you want to check for the existence of a file you have a measure of access to use one of the following: _stat, _stat64, _stati64, _wstat, _wstat64, _wstati64
GetFileAttributes 就是您要查找的内容。如果它返回的值不是 INVALID_FILE_ATTRIBUTES,则该文件存在。
GetFileAttributes is what you're looking for. If it returns a value that is not INVALID_FILE_ATTRIBUTES the file exists.
打开它。您无法可靠地测试文件是否存在于多任务操作系统上。当您打开它时,您可以确保它不会消失。
Open it. You can't reliably test if a file exists on a multi-tasking operating system. When you open it you can make sure it doesn't disappear.
仅限Windows?使用 GetFileAttributes:
或更严格的版本(根据 Szere Dyeri 的评论):
–
Windows only? Use GetFileAttributes:
Or the more strict version (as per Szere Dyeri's comment):
–
在 Windows 代码中,有两种常见的方法可以实现此目的。 GetFileAttributes和CreateFile,
这会告诉你一个文件存在,但它不会告诉你是否有权访问它。为此,您需要使用 CreateFile。
但请记住,即使您从其中一个调用中返回 true,当您打开该文件时,该文件仍然可能不存在。很多时候,最好表现得就像文件存在一样,并在文件不存在时优雅地处理错误。
There are two common ways to do this in Windows code. GetFileAttributes, and CreateFile,
This will tell you a file exists, but but it won't tell you whether you have access to it. for that you need to use CreateFile.
But remember, that even if you get back true from one of these calls, the file could still not exist by the time you get around to opening it. Many times it's best to just behave as if the file exists and gracefully handle the errors when it doesn't.
这是众多选项之一:
Here's one of many options:
我使用 FindFirstFile / FindNextFile 用于此目的的 API 函数。
I use the FindFirstFile / FindNextFile API functions for this purpose.
根据我的经验,_access() 很简单并且相当可移植
In my experience, _access() is simple and fairly portable
这应该可以做到。
This should do it.
我通常使用 boost::filesystem。有一个exists()函数。 :)
I usually use boost::filesystem. Has an exists() function. :)