c 判断文件或文件夹是否存在,多种方法, 为什么从一开始就不直接来个统一的呢?

具体内容,请看: https://blog.csdn.net/u012494876/article/details/51204615

判断文件或文件夹是否存在,竟然有这么多方法:

GetFileAttributes()

CreateFile()

_access()

FindFirstFile()

PathFileExists()

但好像没有一个又简单又100%精准的 api。

之前,判断一个文件夹是否存在,我使用的是:

struct stat info;
return stat(szPath, &info) == 0 && S_ISDIR(info.st_mode);

但今天发现,不支持 windows 短地址模式: C:UsersADMINI~1AppDataLocalTemp

今天,只好使用 GetFileAttributes() 改写了一下:

DWORD dwAttrib = GetFileAttributes(path);
return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);

但看网上的资料,说 GetFileAttributes() 在网络共享环境下,存在 bug ...

原文地址:https://www.cnblogs.com/personnel/p/9903028.html