我必须创建一个包含在特定目录中的文件列表,我已经完成了下面的代码(一个更大的程序的一部分),但是我希望我的程序忽略目录中可能包含的任何文件夹。

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


int main ()
{
  DIR *dirptr;
  struct dirent *entry;
  dirptr = opendir ("synchedFolder");



  if (dirptr != NULL)
  {
    while (entry = readdir (dirptr))
     {
         if(strcmp(entry->d_name,"..")!=0 && strcmp(entry->d_name,".")!=0)
          puts (entry->d_name);

     }

    (void) closedir (dirptr);
  }
  else
    perror ("ERROR opening directory");



}

最佳答案

如果只列出文件,而不列出目录,则必须添加以下检查:

entry->d_type == DT_REG


entry->d_type != DT_DIR

关于c - 仅获取c/Ubuntu中目录中包含的文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6648633/

10-15 19:19