有没有办法从目录中读取两个路径?

时间:2019-11-23 09:08:08

标签: c++ file

我想在计算机上读取两条路径并将它们打印到一个文本文件中。

我正在使用filesystem库,有人建议我将路径添加到vector,但是我对vector没有任何经验。

我的问题是我不能在for loop中放入两条路径,我将代码放入函数中,并且当我运行程序时,如果没有内存,则会出现运行时错误。电脑。

如何读取两个路径并将它们放入文本文件中?

#include <iostream>
#include <filesystem>
#include <fstream>

namespace fs = std::filesystem;
using namespace std;

void fileslist()
{
    ofstream file;

    string pathdesktop = "C:\\Users\\roile\\Desktop\\";
    string pathdownloads = "C:\\Users\\roile\\Downloads\\";

    for (const auto& entry : fs::directory_iterator(pathdownloads + pathdesktop)) {
        file.open(pathdesktop + "YourFiles.txt");
            file << entry.path();
    }

    file.close();
}

int main()
{
    fileslist();
}

我的程序正在获取特定目录中的文件,打开一个文本文件,然后将目录中的文件打印到文本文件中。 我已经打开并关闭了for loop

之外的文件

1 个答案:

答案 0 :(得分:1)

您为两个路径的串联所给定的路径创建目录迭代器,不存在nox。

使用两个嵌套的for循环

stylelint --fix

也请不要使用for(const auto &p: {pathdownloads, pathdesktop}) { for(const auto &entry: fs::directory_iterator(p)) { // do the work } }

此外:在外部for循环中声明文件,然后从using namespace std;直接对其进行初始化。