【问题标题】:List regular files only (without directory) problem仅列出常规文件(无目录)问题
【发布时间】:2010-12-10 15:59:33
【问题描述】:

你知道为什么某些文件没有被这个程序列出,即使它们是“常规的”?:

#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <dirent.h>

int main(void) {
  DIR *dh = opendir("./"); // directory handle
  struct dirent *file; // a 'directory entity' AKA file    
  struct stat info; // info about the file.
  while (file = readdir(dh)) {
    stat(file->d_name, &info);
    printf("note: file->d_name => %s\n", file->d_name);
    printf("note: info.st_mode => %i\n", info.st_mode);
    if (S_ISREG(info.st_mode))
      printf("REGULAR FILE FOUND! %s\n", file->d_name);
  }
  closedir(dh);

  return 0;
}

执行这个程序后,我得到了这个:

note: file->d_name => .
note: info.st_mode => 16877
note: file->d_name => ..
note: info.st_mode => 16832
note: file->d_name => .DS_Store
note: info.st_mode => 16832
note: file->d_name => ef efeff
note: info.st_mode => 16832
note: file->d_name => ffffff
note: info.st_mode => 16832
note: file->d_name => ffffff - copie
note: info.st_mode => 16832
note: file->d_name => folder
note: info.st_mode => 16832
note: file->d_name => printiie.tt
note: info.st_mode => 16832
note: file->d_name => test.c
note: info.st_mode => 33188
REGULAR FILE FOUND! test.c
note: file->d_name => z
note: info.st_mode => 33188
REGULAR FILE FOUND! z

如您所见,程序只看到两个文件。但是每个文件都是有规律的,只有一个文件夹。

这里是shell命令的副本:$ ls -lai

total 64
2421444 drwxr-xr-x  10 denis  staff   340 27 oct 22:19 .
2416789 drwxr-xr-x@ 28 denis  staff   952 27 oct 22:20 ..
2423204 -rw-r--r--@  1 denis  staff  6148 27 oct 21:41 .DS_Store
2423206 -rwxr-xr-x@  1 denis  staff   895 27 oct 19:57 ef efeff
2423183 -rwxr-xr-x@  1 denis  staff   895 27 oct 19:57 ffffff
2423216 -rwxr-xr-x@  1 denis  staff   895 27 oct 19:57 ffffff - copie
2423436 drwxr-xr-x   2 denis  staff    68 27 oct 21:57 folder
2423180 -rw-r--r--@  1 denis  staff    38 27 oct 21:32 printiie.tt
2423682 -rw-r--r--@  1 denis  staff   895 27 oct 19:57 test.c
2423208 -rwxr-xr-x@  1 denis  staff    34 27 oct 21:39 z

我只想列出每个文件的名称,但没有目录。我在 Mac OS X 上工作,但我认为这不是问题的原因。

【问题讨论】:

    标签: c posix directory readdir stat


    【解决方案1】:

    我曾经不得不列出目录中的每个 yml 文件,包括子目录。它在 Win XP 上: system("dir C:\Path\To\File\*.yml /B /S &gt;&gt; list.txt"); 然后我会解析 list.txt 以获取所有这些。一行是一个文件。简单,但不便携。那也是不久前的事了——现在我可能会尝试提升。

    编辑: 这个答案与代码无关,而是:

    其实我只是想列出每个文件的名称,但没有目录。

    如果他采取与我相同的方法,他的问题就会解决。

    【讨论】:

      【解决方案2】:

      对于某些文件,stat 函数似乎失败了,因此info 结构没有被更新,其中仍然包含来自".." 的数据。将该行替换为:

      if (stat(file->d_name, &info))
      {
          printf("error: stat(%s): %s\n", file->d_name, strerror(errno));
          continue;
      }
      

      ..你会明白为什么。

      【讨论】:

      • 谢谢!我收到未列出文件的消息:错误:stat(printiie.tt):没有这样的文件或目录目前我不明白为什么,因为这些文件存在......
      • 在我看来,您使用opendir 检查的目录与stat 正在查看的当前工作目录不同。 opendir(".") 会发生什么?
      • 经过一些测试,我得到了这个:注意:file->d_name =>。 note: info.st_mode => 16877 note: file->d_name => .. note: info.st_mode => 16832 很奇怪,删除.DS_Store后,程序检测到0个文件。
      • stat 成功返回 0,失败返回 -1,因此接受的代码块不正确。 linux.die.net/man/2/stat
      • @somekindarukus:这里的if 语句的主体应该在stat() fails 时执行 - 它显示错误是什么,然后跳到下一个循环的迭代。代码是正确的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 2011-09-04
      • 2012-04-15
      • 1970-01-01
      相关资源
      最近更新 更多