2015-04-07 37 views
0

任何人都可以告訴我如何使用C中的結構數組來保存目錄路徑。在下面的代碼中,任何人都可以告訴哪裏是需要改變嗎?如何使用c將目錄的文件名添加到數組結構中

#include <sys/types.h> 
#include <sys/stat.h> 
#include <stdio.h> 
#include <dirent.h> 
#include <sys/dir.h> 
#include <locale.h> 
#include <stdint.h> 
#include <string.h> 

#define FALSE 0 
#define TRUE ! FALSE 

typedef struct{ 
    char *path; 
}filepath; 

struct stat sb; 
extern int alpahsort(); 
int dir_detect(char *name); 

int main (int argc, char *argv[]) 
{ 
    filepath my_array_path[100]; 
    char *each_name; 
    const char *pathname=NULL; 
    char success; int ret=0; 
    struct direct **files; 
    int j=0,i,count,count_dir; 
    int file_select(); 
    if (argc != 2) { 
     fprintf(stderr, "Usage: %s <pathname>\n", argv[0]); 
     exit(EXIT_FAILURE); 
    } 
    printf("%s\n",argv[1]); 
    pathname=argv[1]; 
    printf("%s\n",pathname); 
    DIR *dp; 
    struct dirent *ep; 
    dp = opendir (pathname); 

    count = scandir(pathname, &files, file_select, alphasort); 

    if (dp != NULL) 
    { 
     while ((ep = readdir (dp))!=NULL){ 
      printf("the number of files=%d\n",count); 
      char *buffer; 


      //from here .... 
      //my_array_path[i].path=malloc(strlen(buffer+1)); 
      //strcpy(my_array_path[i].path,buffer); 
      my_array_path[i].path=strdup(ep->d_name); 
      printf("the name of the file is %s\n",my_array_path[i].path); 
      // to here...... 

我想知道我在做什麼是正確與否。其他代碼如下。

+0

提高你的問題得到明確的和快速的解答。 – Kumar

+1

我們不會爲您編寫代碼。你有所有你需要的提示。試一試。 – chqrlie

+0

實際上我沒有寫過很長的代碼,我想沒有把所有的東西放在一起。我只被卡在只有地方?馬克已被放置。 – manjunath

回答

0

C提供了2種主要方法來讀取目錄內容,類似於2種方法來讀取文件。就像您選擇讀低級read/write無緩衝輸入例程的文件一樣,或者使用FILE*文件流讀取文件,您也可以在讀取目錄時使用相同的選項。

使用目錄,你可以選擇readdir每個調用該函數將一個指針返回到direct struct在目錄中的文件,移動文件位置指示器到下一個文件被讀取。您必須重複呼叫readdir才能訪問目錄中的所有文件。

第二種類型的目錄的訪問依賴於該scandir返回一個指針陣列包含的所有文件/目錄從該目錄中讀取dirent structs。使用scandir,只需遍歷返回的數組即可訪問目錄中包含的每個文件/目錄。

所以這兩種方法的主要區別是readdir返回指向文件的單個指針,scandir返回指向包含目錄中所有文件的數組的指針。 scandir也爲文件/目錄列表提供了幾個預定義的排序例程。 (alphasortversionsort)爲排序direct struct entries提供了一種便捷的方法。請注意,要使用預定義的排序例程,您必須在代碼中包含一個定義到_BSD_SOURCE

下面顯示一個小目錄使用scandir與預定義alphasort的例子:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <sys/types.h> /* opendir */ 
#include <dirent.h>  /* opendir, readdir, scandir */ 
#include <errno.h> 

#ifndef _BSD_SOURCE  /* for scandir sort routines */ 
#define _BSD_SOURCE 
#endif /* _BSD_SOURCE */ 

int sdfilt (const struct dirent *de); 

int main (int argc, char **argv) { 

    if (argc < 2) 
     fprintf (stderr, "warning: usage: %s [dirname (default '.')][mode (default 14)\n", argv[0]); 


    char *dname = (argc > 1) ? argv[1] : "."; /* directory name to get listing of */ 
    struct dirent **namelist = NULL;   /* dirent structure to hold listing */ 
    int ndir = 0;        /* num dirs scandir returns, -1 err */ 
    size_t it = 0;        /* simple iterator for dir list  */ 

    /* call scandir to fill pointer to array of dirent entries */ 
    if ((ndir = scandir (dname, &namelist, sdfilt, alphasort)) < 0) 
    { 
     perror("scandir"); /* throw error & return on failure */ 
     return 1; 
    } 

    /* print each of the entries in alphasort order */ 
    printf ("\nscandir example (alphasort):\n\n"); 
    for (it = 0; it < ndir; it++) 
     printf(" nl[%2zu] %s\n", it, namelist[it]->d_name); 

    /* print each entry in reverse sort order & free */ 
    printf ("\nreverse:\n\n"); 
    it = ndir; 
    while (it--) { 
     printf(" nl[%2zu] %s\n", it, namelist[it]->d_name); 
     if (namelist[it]->d_name) 
      free (namelist[it]); 
    } 
    free(namelist); 

    printf ("\n"); 

    return 0; 
} 

/* simple scandir filter that omit strings and 
dot files '.' and '..' from dirent entries */ 
int sdfilt (const struct dirent *de) 
{ 
    if (strcmp (de->d_name, ".") == 0 || strcmp (de->d_name, "..") == 0) 
     return 0; 
    else 
     return 1; 
} 

輸出

$ ./bin/scandir_simple tmp 

scandir example (alphasort): 

    nl[ 0] bin 
    nl[ 1] d1 
    nl[ 2] d2 
    nl[ 3] rdrmstat.c 
    nl[ 4] rmftw-io-out.txt 
    nl[ 5] walk-ftw-test.c 
    nl[ 6] walk-nftw-test.c 

reverse: 

    nl[ 6] walk-nftw-test.c 
    nl[ 5] walk-ftw-test.c 
    nl[ 4] rmftw-io-out.txt 
    nl[ 3] rdrmstat.c 
    nl[ 2] d2 
    nl[ 1] d1 
    nl[ 0] bin 
0

您可以使用動態內存分配來完成此操作。

聲明變量,

filepath mypath; 

考慮buffer有一個目錄的路徑,然後做這樣的,

int i=0; 
mypath[i].path=malloc(strlen(buffer+1); 
strcpy(mypath[i].path,buffer); 

否則strdup功能,

mypath[i].path=strdup(buffer); 

然後增加該變量以存儲下一個路徑。並檢查不超過您在結構中給出的值的條件。

typedef struct{ 
    char *path[255]; 
}filepath; 

//in main 

filepath mypath; 
char *buffer=malloc(255); 
int i=0; 
strcpy(buffer,"/tmp"); 
mypath[i].path=malloc(strlen(buffer)+1); 
strcpy(mypath[i].path,buffer); 
printf("path:%s\n",mypath[i].path); 
+0

這是給我分段錯誤..... – manjunath

+0

你如何使用? –

+0

typedef struct {char * path; } filepath; filepath mypath; char * buffer; (i = 1; i manjunath

相關問題