How do I get a list of files in a directory in C++?

后端 未结 13 1499
南旧
南旧 2020-11-28 23:36

How do you get a list of files within a directory so each can be processed?

13条回答
  •  余生分开走
    2020-11-28 23:49

    C++11/Linux version:

    #include 
    
    if (auto dir = opendir("some_dir/")) {
        while (auto f = readdir(dir)) {
            if (!f->d_name || f->d_name[0] == '.')
                continue; // Skip everything that starts with a dot
    
            printf("File: %s\n", f->d_name);
        }
        closedir(dir);
    }
    

提交回复
热议问题