簡體   English   中英

C++:獲取並列出用戶寫入的目錄中的所有文件

[英]C++: Get and list all the files from directory that user writes

我剛來這地方。 我正在開發一個用於二進制打包文件的小程序,但我不知道如何制作一個 function 來讀取和列出用戶寫入的文件夾中具有不同名稱和擴展名的所有文件。

string folder;
cout << "Input folder name: ";
getline (cin, folder);

因此,在該命令之后,function 假設使用“文件夾”和當前目錄位置來獲取該文件夾並讀取並列出其中的所有文件。

感謝您的時間和幫助。

我會使用std::filesystem::directory_iterator到 go 通過給定目錄中的所有文件。

#include <iostream>
#include <filesystem>

int main()
{
    const std::filesystem::path path{ "C:\\temp" };
    for (auto const& dir_entry : std::filesystem::directory_iterator{ path })
        std::cout << dir_entry << '\n';
}

要僅獲取文件名(不帶路徑),請使用:

    std::cout << dir_entry.path().filename() << '\n';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM