これは、特定のディレクトリで通常のファイルを探し、それらへのフルパスをリストに格納する私の関数です。
static my_func(const char *path, Files **list) //list - storage for file names
{
DIR *d;
struct dirent *dir;
char buf[PATH_MAX + 1];
d = opendir(path);
if (d) {
while ((dir = readdir(d)) != NULL) {
if ((DT_REG == dir->d_type)) {
realpath(dir->d_name, buf);
List_push(list, buf);
printf("%s\n", dir->d_name);
// memset(buf, 0, PATH_MAX + 1);
}
}
}
closedir(d);
return 0;
}
...
...
int main()
{
// list creation
// my_func call
...
List_print(...)
}
期待される出力:
FILE_1.txt
FILE_2.txt
FILE_3.txt
FILE_4.txt
FILE_5.txt
/home/user/c/FILE_1.txt
/home/user/c/FILE_2.txt
/home/user/c/FILE_3.txt
/home/user/c/FILE_4.txt
/home/user/c/FILE_5.txt
現在の出力:
FILE_1.txt
FILE_2.txt
FILE_3.txt
FILE_4.txt
FILE_5.txt
/home/user/c/FILE_1.txt
/home/user/c/FILE_1.txt
/home/user/c/FILE_1.txt
/home/user/c/FILE_1.txt
/home/user/c/FILE_1.txt
リンクリストの実装に関連できますか? 以下でテストしたため、正常に動作します。
List_push(list, dir->d_name)
と期待通りの結果が得られました。これは List_push の実装です (Files は単純char *
に次の要素へのポインタを持つ構造体です):
void List_push(Files **head, char *x)
{
Files *new;
new = malloc(sizeof(Files));
if (NULL != new) {
new->next = *head;
new->text = x;
*head = new;
} else {
printf("malloc error");
}
}
また、ご覧buf
のとおり、memset でクリアしようとしましたが、成功しませんでした。出力は次のとおりです。
FILE_1.txt
FILE_2.txt
FILE_3.txt
FILE_4.txt
FILE_5.txt
[console]$
はい、空白は何かで埋められているようです (または、これらは'\n'
List_print の単なる記号です)。したがって、リストは空ではありません。
ここで何が問題なのですか?