Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Excevp放入数组以供以后使用_C - Fatal编程技术网

将Excevp放入数组以供以后使用

将Excevp放入数组以供以后使用,c,C,我正在使用execvp查找某些文件。这是我正在使用的代码: char *argv1[] = {"find", "-name", "*.jpg", NULL}; execvp("find",argv1); 我想知道是否有一种方法可以将execvp的结果存储/保存到一个数组中,供以后的用户使用? 还有没有一种方法可以只获取所需的文件而不是整个路径 这是我的readdir function static void list_dir (const char * dir_name) { char

我正在使用execvp查找某些文件。这是我正在使用的代码:

char *argv1[] = {"find", "-name", "*.jpg", NULL};
execvp("find",argv1);
我想知道是否有一种方法可以将execvp的结果存储/保存到一个数组中,供以后的用户使用? 还有没有一种方法可以只获取所需的文件而不是整个路径

这是我的readdir function

static void list_dir (const char * dir_name)
{
    char *ArrayFiles[100];
    DIR * d;
    /* Open the directory specified by "dir_name". */
    int i = 0;
    d = opendir (dir_name);

    /* Check it was opened. */
if (!d) {
    fprintf (stderr, "Cannot open directory '%s': %s\n",
             dir_name, strerror (errno));
    exit (EXIT_FAILURE);
}
while (1) {
    struct dirent * entry;
    const char * d_name;

    /* "Readdir" gets subsequent entries from "d". */
    entry = readdir (d);
    if (! entry) {
        /* There are no more entries in this directory, so break
           out of the while loop. */
        break;
    }

   d_name = entry->d_name;
   if ((strstr(d_name, ".jpg")) || (strstr(d_name, ".JPG"))){
/* skip printing directories */
    if (! (entry->d_type & DT_DIR)) {
    char *filename = entry->d_name;
    printf ("%s\n", d_name); 
}
}


为什么不使用readdir并检查每个条目呢?iharob:我使用readdir来读取所需文件的目录和子目录,但对我来说,放入数组很复杂,主要是因为我使用递归方法读取子目录。我只是想知道这是否是可以做到的。任何事情都可以做到,这只是找到方法的问题,你能发布你的readdir代码吗?那么你想让listdir函数返回一个包含所有匹配文件的数组吗?为什么要在c中这样做呢?我必须将函数返回的文件用于一个更大的项目,但我需要将其存储到一个数组中才能使用它们。我似乎无法在主程序中传递目录,当返回所需的文件时,将它们存储到数组中以供其他使用。
     if (entry->d_type & DT_DIR) {


            /* skip the root directories ("." and "..")*/
            if (strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) {

                int path_length;
                char path[PATH_MAX];

                path_length = snprintf (path, PATH_MAX,
                                        "%s/%s", dir_name, d_name);
                if (path_length >= PATH_MAX) {
                    fprintf (stderr, "Path length has got too long.\n");
                    exit (EXIT_FAILURE);
                }
                /* Recursively call "list_dir" with the new path. */
                list_dir (path);
            }
        }

}

    /* After going through all the entries, close the directory. */
    if (closedir (d)) {
        fprintf (stderr, "Could not close '%s': %s\n",
                 dir_name, strerror (errno));
        exit (EXIT_FAILURE);
    }
}