在c++中逐行复制
Copy line by line in C++
我正在尝试将2个文件复制到1个文件中ID1。name1。ID2。name2。但是我做不到……我该如何从每个文件中复制每一行呢?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream file1("ID.txt");
ifstream file2("fullname.txt");
ofstream file4("test.txt");
string x,y;
while (file1 >> x )
{
file4 << x <<" . ";
while (file2 >> y)
{
file4 << y <<" . "<< endl;
}
}
}
首先,逐行阅读。
ifstream file1("ID.txt");
string line;
ifstream file2("fulName.txt");
string line2;
while(getline(file1, line))
{
if(getline(file2, line2))
{
//here combine your lines and write to new file
}
}
我会把每个文件分别处理成它们自己的列表。然后将列表的内容放入组合文件中。
ifstream file1("ID.txt");
ifstream file2("fullname.txt");
ofstream file4("test.txt");
std::list<string> x;
std::list<string> y;
string temp;
while(getline(file1, temp))
{
x.add(temp);
}
while(getline(file2, temp))
{
y.add(temp);
}
//Here I'm assuming files are the same size but you may have to do this some other way if that isn't the case
for (int i = 0; i < x.size; i++)
{
file4 << x[i] << " . ";
file4 << y[i] << " . ";
}
相关文章:
- 如何在c++中从文本文件中逐行读取整数
- 打印逐行存储的文本文件
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- C++如何将字符串逐行转换为双精度
- 在 c++ 中逐行阅读字符串,不要以空格停止
- 使用 QT 逐行读取 JSON
- 逐行读取文件,并将数据插入变量和数组中
- 逐行从文本文件中读取 |C++
- 我遇到使用 c++ 逐行读取输入文件的问题
- 无法逐行读取.csv文件
- 在 c++ 中逐行读取文件(将代码从 Python 转换为 C++)
- 如何逐行从文件中获取单词,并在C++中用分号分隔?
- C ++ txt 文件成数组逐行字符串和 int 用于纸牌游戏
- 如何将文件逐行读取到矢量中,然后打印矢量
- C++逐行读取文件
- 如何逐行读取文件并分隔行组件?
- C++ 逐行从文件(包含空格)读取数据
- 对包含整数的文本文件进行排序时,必须逐行进行排序
- 逐行复制数组的内容
- 在c++中逐行复制