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

In c++, \"DOI.txt\" which conatins the Declaration of Independence of United Sta

ID: 3685719 • Letter: I

Question

In c++, "DOI.txt" which conatins the Declaration of Independence of United States of America.

Write a function "fileprocess()" that:

1. Reads each line in DOI.txt (open and read)

2. Finds the position of word "Government" in read line.

3. Write to "Results.txt", the line number and the position of the word. Ex: If the word is found in line 10 at position 20, write "Word found in Line 10 Position 20" into "Results.txt" Make sure to have a newline(enter) after writing each line.

4. At the end of file, write the number of occurances of the word to "Results.txt". Ex: "Word Government occured 'n' times"

5. Repeat the above process for the word "people" and write the output to "Results.txt"

Note: Template is missing Opening and Closing of files inside main() function and must be completed. As a hint, comments are provided inside main.

Program Template: int main()

{ //open Input file// //Check if output file exist and open in trunc mode//

fileprocess(myfile,outfile,"Government"); //close and re-open the Input file//

fileprocess(myfile,outfile,"people"); //Close file after processing//

return 0; }

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>


using namespace std;
void fileprocess1(ifstream& ,ofstream& ,string ); //declaration of function
int main(int argc, char *argv[])
{
   
    ifstream in;
    in.open("DOI.txt"); //opening input file
    ofstream outfile("results.txt",ios::trunc); //openg output file in trunc mode
  
    fileprocess1(in,outfile,"Government");//calling method
    fileprocess1(in,outfile,"People"); //calling method
  

    in.close();
  
  
  
    system("PAUSE");
    return EXIT_SUCCESS;
}

//function declaration
void fileprocess1(ifstream &myfile,ofstream &outfile,string word)
{
     int countWord=0;
     int countLine=0;
     string line;
   
     //fetch each line one by one
   
    while(getline(myfile,line))
    {
      countLine++;
      size_t found;
      found=line.find(word);     // search of particular word in that line
      if (found!=string::npos) //if word found, store its position
      {
      countWord++;
      
        //writing data to output file
       outfile<


myfile.open("input.txt");//re opening file
myfile.clear();
myfile.seekg (0, ios::beg); // placing file pointer to top of file.


}

I'm getting an error "sh: PAUSE: command not found"

Explanation / Answer

#include <iostream.h>

#include <cstdlib.h>
#include <fstream.h>

#include <string.h>

using namespace std;
void fileprocess1(ifstream& ,ofstream& ,string ); //declaration of function
int main(int argc, char *argv[])
{
   
    ifstream in;
    in.open("DOI.txt"); //opening input file
    ofstream outfile("results.txt",ios::trunc); //openg output file in trunc mode
  
    fileprocess1(in,outfile,"Government");//calling method
    fileprocess1(in,outfile,"People"); //calling method
  

    in.close();
  
  
  
    system("PAUSE");
    return EXIT_SUCCESS;
}

//function declaration
void fileprocess1(ifstream &myfile,ofstream &outfile,string word)
{
     int countWord=0;
     int countLine=0;
     string line;
   
     //fetch each line one by one
   
    while(getline(myfile,line))
    {
      countLine++;
      size_t found;
      found=line.find(word);     // search of particular word in that line
      if (found!=string::npos) //if word found, store its position
      {
      countWord++;
      
        //writing data to output file
       outfile<


myfile.open("input.txt");//re opening file
myfile.clear();
myfile.seekg (0, ios::beg); // placing file pointer to top of file.


}

Note : Now try the program. Even though it gives an error delete the line system(pause) and check the output.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote