2015-04-07 39 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

相关问题