如何合并不同文本列的多个列

时间:2014-07-07 15:32:15

标签: c++ csv text

我有多个文本文件,我从一个文本文件中分离出来,现在我想将它们与,分隔的表单合并,但列中包含序列

DATA1.TXT

A man in the park

A man in the ground

A man is running

data2.txt

Yes1

No1

Why not1

data3.txt

Yes2
No2
Why not2

data4.txt

Yes3
No3
Why not3

data5.txt

Yes4
No4
Why not4

如何使用将这些数据文件合并为逗号分隔值()文件?

1,A man in the park,Yes1,Yes2,Yes3,Yes4
2,A man in the ground,No1,No2,No3,No4
3,A man is running,Why not1,Why not2,Why not3 , Why not4

我看到related有关它的问题,但它与

无关

1 个答案:

答案 0 :(得分:0)

您是否尝试过一次打开所有文件并为每个输入文件使用一个字符串?

unsigned int line_number = 1;
ifstream file1("data1.txt");
ifstream file1("data2.txt");
ifstream file1("data3.txt");
ifstream file1("data4.txt");
ifstream file1("data5.txt");
ofstream out("output.txt");

std::string input1;
std::string input2;
std::string input3;
std::string input4;
std::string input5;

getline(file1, input1);
getline(file2, input2);
getline(file3, input3);
getline(file4, input4);
getline(file5, input5);


out << line_number
    << ", " << input1
    << ", " << input2
    << ", " << input3
    << ", " << input4
    << ", " << input5
    << "\n";

以上代码是基础知识。读者和OP需要将代码放入循环并添加错误检查。这是以“批处理”模式构建的,其中许多相关操作聚集在一起。

是的,上面的代码可以简化,但仅供参考。