首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何合并不同文本列的多个列

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

Stack Overflow用户
提问于 2014-07-07 15:32:15
回答 1查看 609关注 0票数 0

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

data1.txt

代码语言:javascript
复制
A man in the park

A man in the ground

A man is running

data2.txt

代码语言:javascript
复制
Yes1

No1

Why not1

data3.txt

代码语言:javascript
复制
Yes2
No2
Why not2

data4.txt

代码语言:javascript
复制
Yes3
No3
Why not3

data5.txt

代码语言:javascript
复制
Yes4
No4
Why not4

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

代码语言:javascript
复制
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

我看到了相关关于它的问题,但它与c++无关

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-07 15:49:04

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

代码语言:javascript
复制
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需要将代码放入循环并添加错误检查。这是以“批处理”模式构造的,其中许多相关操作被聚集在一起。

是的,上面的代码可以简化,但只是为了说明。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24614349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档