【问题标题】:Get only the files included in a directory in c / Ubuntu仅获取 c / Ubuntu 中目录中包含的文件
【发布时间】:2011-10-02 16:20:36
【问题描述】:

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

#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");



}

【问题讨论】:

  • 你不能使用 shell 脚本吗?

标签: c linux file list directory


【解决方案1】:

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

entry->d_type == DT_REG

entry->d_type != DT_DIR

【讨论】:

    【解决方案2】:

    stat()lstat()return value for stat。在后者中,请注意 S_ISDIR 宏。

    【讨论】:

      【解决方案3】:

      简短的回答是不同的结构包含必要的信息:

      if ( entry->d_type == DT_REG)
      

      【讨论】:

        【解决方案4】:

        检查 stat(或 lstat)

        #include <stdio.h>
        #include <stdlib.h>
        #include <sys/types.h>
        #include <sys/stat.h>
        
        
        
        
        
        /* int main (void){ */
        int main (int argc, char **argv){
            int i,result=0;
            struct stat buf;
        
            /* print_S_I_types(); */
        
        
            for (i=1; i < argc; i++){
                if (lstat(argv[i], &buf) < 0) {
                    fprintf(stderr, "something went wrong with %s, but will continue\n",
                           argv[i]);
                    continue;
                } else {
                    if S_ISREG(buf.st_mode){ 
        
                        printf("argv[%d] is normal file\n",i);
        
        
                    }else {
                      printf("argv[%d] is not normal file\n",i);
                    }
                }
            }
        
            return 0;
        }
        

        【讨论】:

          【解决方案5】:

          列出文件的工作代码(无目录):

          #include <stdio.h>
          #include <dirent.h>
          #include <stdlib.h>
          
          int main()
          {
              DIR *dir;
              struct dirent *ent;
              if ((dir = opendir ("/home/images")) != NULL) 
              {
                  /* print all the files and directories within directory */
                  while ((ent = readdir (dir)) != NULL) 
                  {
                      if(ent->d_type!= DT_DIR)
                      {
                          printf ("%s\n", ent->d_name);
                      }
                  }   
                  closedir (dir);
              }
              else 
              {
                  /* could not open directory */
                  perror ("");
                  return EXIT_FAILURE;
              }
          } 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-07-11
            • 2013-05-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多