我试图找出某些文件是否在某个文件夹中。但是,即使文件存在,我尝试查找它们的方法在某些文件夹中也不起作用。
bool FileExists(string strFilename) {
struct stat stFileInfo;
bool blnReturn;
int intStat;
intStat = stat(strFilename.c_str(),&stFileInfo);
if(intStat == 0) {
// We were able to get the file attributes
// so the file obviously exists.
blnReturn = true;
printf("Found file %s\n", strFilename.c_str());
} else {
blnReturn = false;
printf("Didn't find file %s\n", strFilename.c_str());
}
return(blnReturn);
}
当我在/mnt/ram 中挂载一个目录时,它不会(有时会)在那里找到文件,但是当我使用磁盘上的另一个目录时,它总是能找到文件。
有没有其他方法可以查看目录中是否存在文件?
谢谢
最佳答案
stat
调用或任何其他目录/文件列表的结果取决于调用进程的权限。 /mnt/ram
可能对当前用户是隐藏的。
如评论中所述,opendir
和 readdir
是获取(递归)目录列表的惯用方式。显然,stat
是成语 :-)
的一部分。
关于c++ - 比 stat() 更好的方法来查找目录中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13899982/