0

ディレクトリ内のすべてのファイルを再帰的に一覧表示しようとしています。しかし、私のコードはエラーを出さずにクラッシュします。指定されたファイルがディレクトリの場合、関数を再帰的に呼び出すか、その名前を出力します。

私はdirent.hを使用しています

int list_file(string path)
{
    DIR *dir;
    struct dirent *ent;
    char  *c_style_path;
    c_style_path = new char[path.length()];
    c_style_path = (char *)path.c_str();
    dir = opendir (c_style_path);
    if (dir != NULL) {

        /* print all the files and directories within directory */
        while ((ent = readdir (dir)) != NULL) {
            if(ent->d_type == DT_DIR && (strcmp(ent->d_name,".")!=0) && (strcmp(ent->d_name,"..")!=0))
            {
                string tmp = path + "\\" + ent->d_name;
                list_file(tmp);
            }
            else
            {
                cout<<ent->d_name<<endl;
            }
        }
        closedir (dir);
    } else {
        /* could not open directory */
        perror ("");
        return EXIT_FAILURE;
    }
    delete [] c_style_path;
    return 0;
}

ここで何が間違っているのかわかりません。手がかりはありますか?

4

1 に答える 1