2

我正在尝试在 C 中列出给定目录中的所有文件和文件夹,出现以下代码错误,我不知道出了什么问题

#include <sys/types.h>
#include <dirent.h>
#include <regex.h>
#include <stdio.h>

#include <unistd.h>
#include <pwd.h>

enum {
    WALK_OK = 0,
    WALK_BADPATTERN,
    WALK_BADOPEN,
};

int walk_directories(const char *dir, const char *pattern, char* strings[])
{
    struct dirent *entry;
    regex_t reg;
    DIR *d; 
    int i = 0;
    //char array[256][256];

    if (regcomp(&reg, pattern, REG_EXTENDED | REG_NOSUB))
    return WALK_BADPATTERN;
    if (!(d = opendir(dir)))
    return WALK_BADOPEN;
    while (entry = readdir(d))
    if (!regexec(&reg, entry->d_name, 0, NULL, 0) )
            //puts(entry->d_name);
        strings[i] = (entry->d_name);
        i++;
    closedir(d);
    regfree(&reg);

    return WALK_OK;
}

void main()
{
    struct passwd *pw = getpwuid(getuid());
    char *homedir = pw->pw_dir;
    strcat(homedir, "/.themes");

    int n = 0;
    char *array[256][100];
    char *array2[256][100];

    walk_directories(homedir, "", array);
        for (n = 0; n < 256; n++)
        {
            //do stuff here later, but just print it for now
            printf ("%s\n", array[n]);
        }

    walk_directories("/usr/share/themes", "", array2);
        for (n = 0; n < 256; n++)
        {
            //do stuff here later, but just print it for now
            printf ("%s\n", array2[n]);
        }
}

编译时的错误是

test2.c: In function ‘main’:
test2.c:42:2: warning: incompatible implicit declaration of built-in function ‘strcat’ [enabled by default]
test2.c:48:2: warning: passing argument 3 of ‘walk_directories’ from incompatible pointer type [enabled by default]
test2.c:15:5: note: expected ‘char **’ but argument is of type ‘char * (*)[100]’
test2.c:52:6: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]
test2.c:55:2: warning: passing argument 3 of ‘walk_directories’ from incompatible pointer type [enabled by default]
test2.c:15:5: note: expected ‘char **’ but argument is of type ‘char * (*)[100]’
test2.c:59:6: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat]

如果有帮助,我已经在 python 中实现了我想要的,这是 C 的期望结果

import os
DATA_DIR = "/usr/share"

def walk_directories(dirs, filter_func):
    valid = []
    try:
    for thdir in dirs:
        if os.path.isdir(thdir):
            for t in os.listdir(thdir):
                if filter_func(os.path.join(thdir, t)):
                     valid.append(t)
    except:
    logging.critical("Error parsing directories", exc_info=True)
    return valid

def _get_valid_themes():
    """ Only shows themes that have variations for gtk+-3 and gtk+-2 """
    dirs = ( os.path.join(DATA_DIR, "themes"),
         os.path.join(os.path.expanduser("~"), ".themes"))
    valid = walk_directories(dirs, lambda d:
            os.path.exists(os.path.join(d, "gtk-2.0")) and \
            os.path.exists(os.path.join(d, "gtk-3.0")))
    return valid

print(_get_valid_themes())

谢谢你

[编辑] 感谢您的帮助,我现在唯一的问题是 printf 全部吐出垃圾而不是我所期望的,我尝试了一些事情,while 循环现在看起来像这样

    while (entry = readdir(d))
    if (!regexec(&reg, entry->d_name, 0, NULL, 0) )
            //printf("%s\n",entry->d_name);
        strcpy(strings[i], (entry->d_name));
        //strings[i] = (entry->d_name);
        printf("%i\n",i);
        i++;
    closedir(d);

我也没有正确打印,这就是我从 3 个 printf 语句中得到的全部

0
Adwaita2





















\@




0
Radiance

��





\@



�K��
� `���
����
�


��
�
.N=

�O��
�

�

应该提到,如果我启用

       printf("%s\n",entry->d_name);

然后它会打印预期的输出

4

2 回答 2

2
  1. 您应该包括string.h以获取strcat(3).

  2. 在您的声明中:

    int walk_directories(const char *dir, const char *pattern, char* strings[])
    

    char *strings[]只是语法糖的意思char **strings。由于您要传递一个二维数组,因此这是行不通的。在我看来,您打算创建两个字符串数组,但这不是这些声明的作用:

    char *array[256][100];
    char *array2[256][100];
    

    你可能不想要*那里的 s 。如果你把它们取下来,你可以将签名更改为walk_directories

    int walk_directories(const char *dir, const char *pattern, char strings[][100])
    

    它应该可以工作,并在您的函数内部进行必要的更改以匹配。作为奖励,此更改将使您的printf通话也开始工作。

  3. 看起来您的while循环体周围缺少一些大括号。

于 2013-01-22T18:33:59.347 回答
1

第一个警告表明编译器无法确定strcat()函数应该采用哪些参数。由于这是标准 C 函数,因此此警告意味着您缺少#include指令。具体来说,您需要#include <string.h>. 当你修复这个问题时,你可能会发现你得到不同的错误和警告,所以从那里开始工作。

于 2013-01-22T18:35:15.647 回答