Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone write the program for the question below in C++ Programming Language?

ID: 3693714 • Letter: C

Question

Can anyone write the program for the question below in C++ Programming Language? 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. Here is an example. input1.txt: This is the first line in input1.txt. This is the second line in input1.txt. This is the third line in input1.txt. This is the fourth line in input1.txt. input2.txt: This is the first line in input2.txt. This is the second line in input2.txt. This is the third line in input3.txt. outtput.txt This is the first line in input1.txt. This is the first line in input2.txt. This is the second line in input1.txt. This is the second line in input2.txt. This is the third line in input1.txt. This is the third line in input3.txt. This is the fourth line in input1.txt. After the writing in output.txt, count the number of lines in output.txt. Also write the number of times term ‘input1’ appears in output.txt file.

Explanation / Answer

/**
C++ program that reads two input files input1.txt and input2.txt files.
The writes the output to output.txt file. The finds the number of times
the stirng "input1" text appears in the file output.txt file
*/
//header file
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
//function prototype
int count(ifstream &, string );

//main method
int main()
{


   ifstream fin1;
   ifstream fin2;

   //open two input files for eading
   fin1.open("input1.txt");
   fin2.open("input2.txt");

   //open output file for writing
   ofstream fout;
   fout.open("output.txt");

   string line1;
   string line2;
   //Check if files fin1 and fin2 are exist or not
   if(!fin1 || !fin2)
   {
       cout<<"Files do not exist ."<<endl;
       exit(0);
   }


   while(!fin1.eof() &&!fin2.eof())
   {

       getline(fin1,line1,' ');
       getline(fin2,line2,' ');

       fout<<line1<<line2<<endl;
   }

   while(!fin1.eof())
   {
       getline(fin1,line1,' ');      
       fout<<line1<<endl;
   }

   while(!fin2.eof())
   {
       getline(fin2,line2,' ');      
       fout<<line2<<endl;
   }
  

   //close the file objects
   fin1.close();
   fin2.close();
   fout.close();

   ifstream fin;
   //open output text file
   fin.open("output.txt");

   //call count function
   cout<<"input1"<<" string occurs "<<count(fin,"input1")<<" times "<<endl;


   system("pause");
   return 0;
}

/**
The funciton count that takes a ifstream object and search string
and returns the number of times the string search occures in the file
output.txt
*/

int count(ifstream &fin, string search)
{  
   string line;
   int cnt=0;

   //check if file exist or not
   if(!fin)
   {
       cout<<"Files do not exist ."<<endl;
       exit(0);
   }

   while(!fin.eof())
   {
       //read line from file object,fin
       getline(fin,line,' ');  
       //call find method
       int pos=line.find(search);
       //if pos is not -1
       if(pos!=-1)
           //increment the cnt by one
           cnt++;
   }

   //close file object
   fin.close();

   //return cnt
   return cnt;
}

-----------------------------------------------------------------------------------------------------------------------------------------------------

input1.txt

This is the first line in input1.txt.
This is the second line in input1.txt.
This is the third line in input1.txt.
This is the fourth line in input1.txt.

input2.txt

This is the first line in input2.txt.
This is the second line in input2.txt.
This is the third line in input2.txt.

Sample output:

input1 string occurs 4 times

output.txt file

This is the first line in input1.txt. This is the first line in input2.txt.
This is the second line in input1.txt. This is the second line in input2.txt.
This is the third line in input1.txt. This is the third line in input2.txt.
This is the fourth line in input1.txt.