c++ - 比 stat() 更好的方法来查找目录中的文件?

标签 c++ linux ubuntu

我试图找出某些文件是否在某个文件夹中。但是,即使文件存在,我尝试查找它们的方法在某些文件夹中也不起作用。

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 可能对当前用户是隐藏的。

如评论中所述,opendirreaddir 是获取(递归)目录列表的惯用方式。显然,stat 是成语 :-) 的一部分。

关于c++ - 比 stat() 更好的方法来查找目录中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13899982/

相关文章:

c++ - 在 Linux 中为 c++ 使用 gprof -f 选项

c++ - QMetaType::Type来自模板类型

linux - 为 NIS 帐户创建本地邮箱

linux - 将syslog文件复制到linux中的新目录

android - 与 adb 的连接已关闭,并且发生了严重错误。 - Ubuntu 12.04

node.js - 使用 gulp 命令时没有此类文件或目录错误?

linux - 防止将 systemd 服务日志转发到 syslog,而不影响发送到 syslog 的其他服务日志

c++ - 两个线程使用相同的 websocket 句柄——这会导致任何问题吗?

c++ - 仅资源 DLL - 文件夹、路径

c - Linux编程: which device a file is in