Input and output files

Write a program that opens two text files (input1.txt and input2.txt) for
input and one for output (output.txt). The program should merge the two input files in the output files.
While doing the merge, it will read one line from input1.txt and one line from input2.txt
and write those in the output file one after other. Then it will read the next line from input1.txt and
next line from input2.txt and write them one after other in the output file.

This is my code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ofstream myproject;
myproject.open("input1.txt");
myproject<<"This is the first line in input1.txt.\n";
myproject<<"This is the second line in input1.txt.\n";
myproject<<"This is the third line in input1.txt.\n";
myproject<<"This is the fourth line in input1.txt.\n";
myproject.close();

ofstream mypro;
mypro.open("input2.txt");
mypro<<"This is the first line in input2.txt.\n";
mypro<<"This is the second line in input2.txt.\n";
mypro<<"This is the third line in input3.txt.\n";
mypro.close();

ifstream gettFile;
gettFile.open("input1.txt", ios_base:: in);
string message1;
getline(gettFile, message1);
cout<< message1<<"\n";

ifstream getFile;
getFile.open("input2.txt", ios_base:: in);
string messag;
getline(getFile, messag);
cout<< messag<<"\n";

system("pause");
return 0;
}

I need help on:
1. how to get the second line in the input1.txt file and so on.
2. After the writing in output.txt, count the number of lines in output.txt.
3. write the number of times term ‘line’ appears in output.txt file.
Last edited on
first ... always use different objects to open different files simultaneously.
you can use jump statement to get the merged output.
put a statement count=count+2 ; after that jump statement.
You'll get more response if you use code tags.
http://www.cplusplus.com/articles/z13hAqkS/
am new to c++. can u show me the syntax for a jump statement. thank you
Topic archived. No new replies allowed.