2011-07-11 1 views
0

特定のディレクトリ内に含まれるファイルのリストを作成する必要があります。以下のコードを実行しましたが(大きなプログラムの一部ですが)、私のプログラムはディレクトリ内に含まれる可能性のあるフォルダをすべて無視します。c/Ubuntuのディレクトリに含まれているファイルのみを取得する

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



} 
+0

あなたはシェルスクリプトを使用することができませんか? – Makis

答えて

2

あなただけのファイルを一覧表示したいのですが、何のディレクトリは、次のチェックを追加する必要がない場合:

entry->d_type == DT_REG 

または

entry->d_type != DT_DIR 
1

短い答えdirent構造体は、必要な情報が含まれます:

if (entry->d_type == DT_REG) 
0

チェックスタット(またはLSTAT)

012 (ディレクトリなし)のファイルを一覧表示するコードをワーキング
#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; 
} 
0

#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; 
    } 
} 
関連する問題