将所有* .txt文件合并到一个文本文件中,该文件的名称由用户使用C ++给出

问题描述 投票:0回答:1

我正在尝试使用c ++制作一个小而简单的shell,我想使用类似这样的东西。

if (arr[0] == "type")

    {
         system("type *.txt >> output.txt");
    }

如果用户输入type,则程序将把当前工作目录中的所有文本文件合并为一个。当我使用上面的代码运行它时,它将合并文件,但会将那些文本文件的内容两次添加到output.txt。我还可以使用存储的数组值指定“ output.txt”吗?

用户输入命令的方式如下

  getline(cin, command);


        string line = command;             
        string arr[5];
        int i = 0;
        stringstream ssin(line);
        while (ssin.good() && i < 5) {
            ssin >> arr[i];
            ++i;
        }

所以我想用arr [3]代替“ output.txt”

c++ shell file-io
1个回答
1
投票

可能正在发生的是,output.txt也与* .txt匹配,因此在某些时候将所有内容再次写入该文件。您可以轻松地测试将output.txt的扩展名更改为例如output.out。

对于第二个问题,您可以执行以下操作:

ostringstream os;
string out_file_name;
os << ""type *.txt >> " << out_file_name;
system(os.str());

可能会起作用

© www.soinside.com 2019 - 2024. All rights reserved.