从C中的目录和子目录中只读取具有特定扩展名的文件。

时间:2015-03-27 03:46:55

标签: c

我修改了这段代码,但问题是,我无法让它只向我发送带有特定扩展名的文件。我尝试了很多东西,每次都没有得到所需的输出。任何帮助,将不胜感激。这是一个非常大的项目的一部分,我陷入了这个特定的功能。

static void list_dir (const char * dir_name)

{



   DIR * d;

/* Open the directory specified by "dir_name". */

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;
    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") == 0 && !strstr (d_name, ".JPG") == 0){
         continue;
     }
    /* Print the name of the file and directory. */
printf ("%s\n", d_name);


/* If you don't want to print the directories, use the
   following line: */

    //if (! (entry->d_type & DT_DIR)) {
    //printf ("%s/%s\n", dir_name, d_name);
//}


    if (entry->d_type & DT_DIR) {

        /* Check that the directory is not "d" or d's parent. */

        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);
            //printf ("%s\n", path);
            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. */
              if(!strstr (d_name, ".jpg") == 0 && !strstr(d_name, ".JPG") == 0){
         continue;
     }
            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);
}

}

2 个答案:

答案 0 :(得分:3)

您应该知道仅使用".jpg"".JPG"无法解决区分大小写问题,因为可能会更像".jPg"等等。

你可以试试这个

int endsWith(const char *const filename, const char *const extension)
 {
    size_t fileNameLength;
    size_t extensionLength;

   if ((filename == NULL) || (extension == NULL))
       return 0;

    fileNameLength  = strlen(filename);
    extensionLength = strlen(extension);

    return (strcasecmp(extension, filename + filenameLength - extensionLength) == 0);

}

然后你可以做

if (endsWidth(d_name, ".jpg") != 0) /* it has jpeg extension */

注意strcasecmp()不标准,您可以为c库Google搜索找到正确的替代方法。

而且,有些库通过使用所谓的 magic 字节来检查文件类型,它们通常是文件内容中的前4个字节,甚至有一个名为{{ 1}},虽然我从来没有使用它,所以我无法就如何使用它给你建议。

答案 1 :(得分:2)

我会替换

if(!strstr (d_name, ".jpg") == 0 && !strstr (d_name, ".JPG") == 0){

代码更易读:

if( fileIsNotJPGFile(d_name) ) {

并实现以下功能:

int fileIsJPGFile(char const* name)
{
   return (strstr(name, ".jpg") || strstr(name, ".JPG"));
}

int fileIsNotJPGFile(char const* name)
{
   return !(fileIsJPGFile(name));
}
相关问题