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です。2D 配列を渡しているため、機能しません。文字列の 2 つの配列を作成しようとしているように見えますが、それはこれらの宣言が行うことではありません。

    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 に答える