0

i'm new with c++, i want to write a function to open multiple file with same extension in folder, and i write a code like this:

string get_folder(string path, string ext, string folder, string list_name)
{
//Set đường dẫn hiện tại
    auto c_path = filesystem::current_path();
    stringstream set_path;
    set_path << c_path;
    string cur_path;
    set_path >> cur_path;
    cur_path.erase(cur_path.begin(),cur_path.begin()+1);
    cur_path.erase(cur_path.end()-1,cur_path.end());
    cur_path = cur_path + "\\";
    path = cur_path + path;
    folder = cur_path + folder;

//List các file
    cout << "Đang lấy danh sách file\n";
    ofstream list_file(list_name);
    int counter;
    for (auto &p : filesystem::recursive_directory_iterator(path)){
        if (p.path().extension() == ext)
            list_file << p.path().parent_path().stem().string() << "/" << p.path().stem().string() << ext << '\n';
        counter++;
    }
    list_file.close();

//Tạo thư mục xuất
    filesystem::create_directories(folder);

//Lấy danh sách file
    ifstream get_list(list_name);
    string file_list, file_name[counter];
    int i;
    for (i = 1; i < counter; i++){
        while(getline(get_list, file_list)){
            if (file_list.find(ext) != file_list.npos)
                file_name[i] = file_list;
            return file_name[i];
            break;
        }
    }
    get_list.close();
}

but when i tried to call it from main(), it can't return multiple file name like i want, so how could i make this function return multiple file_name[i] to the main() function?

001
  • 13,291
  • 5
  • 35
  • 66
FM39hz
  • 1
  • 2
    Use [`std::tuple`](https://en.cppreference.com/w/cpp/utility/tuple) – Darth-CodeX Mar 08 '22 at 15:01
  • 3
    If the amount is unknown in advance, `std::vector` is your friend. – molbdnilo Mar 08 '22 at 15:03
  • 1
    Use coroutines. And btw this ain't python you mostly need to put braces around your `return`. Because otherwise it's not connected with the previous statement. – AnArrayOfFunctions Mar 08 '22 at 15:03
  • I don't see the point of writing the paths to disk and then reading them back. Again, `std::vector` is your friend. – molbdnilo Mar 08 '22 at 15:07
  • And I suspect that all your complicated path manipulations are already supported, but more reliably, by `filesystem`. (It's not obvious what the point of them are, though.) – molbdnilo Mar 08 '22 at 15:13

0 Answers0