在C++中逐行复制

tau*_*man 1 c++ copy file

我试图将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;
       }
   } 


}
Run Code Online (Sandbox Code Playgroud)

小智 6

首先,逐行阅读.

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
    }
}
Run Code Online (Sandbox Code Playgroud)