任何人都可以告訴我如何使用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......
我想知道我在做什麼是正確與否。其他代碼如下。
提高你的問題得到明確的和快速的解答。 – Kumar
我們不會爲您編寫代碼。你有所有你需要的提示。試一試。 – chqrlie
實際上我沒有寫過很長的代碼,我想沒有把所有的東西放在一起。我只被卡在只有地方?馬克已被放置。 – manjunath