任何人都可以告诉我如何使用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