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

HELP WITH C++ Write the definition of a function named fscopy that does a line-b

ID: 3574917 • Letter: H

Question

HELP WITH C++

Write the definition of a function named  fscopy that does a line-by-line copy from one stream to another. This function can be safely passed two fstream objects , one opened for reading, the other for writing. In addition, it gets a boolargument that is true if the output lines are to be numbered, and another argument , an int that specifies line-spacing in the output .

Assume that the input source is a text file consisting of a sequence of newline character -terminated lines. The function copies, line by line, the entire content of the data source associated with the first argument to the data target associated with the second argument . It returns the number of lines read in. If it the third parameter , the bool is true , every line that is output is preceded by a 6-position field holding the line number (an integer ) followed by a space and then the actual line. If the last argument , the int is 2 or more, it specifies the line spacing: for example, it is 3, there are two blank lines output after each line printed. These blank lines are NOT numbered.

Explanation / Answer

C++ program:

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

//Function to copy files from first stream to second stream
void fscopy(fstream& in, fstream& out, bool printNumbers, int lineSpaces){
   string line, lineNumber;
   ostringstream number;
   int count=1;
   int spaceCount;
  
   //   Field size to store line numbers. 6 in this case
   int fieldSize=6;
   while(getline(in, line)){
       number<<count;
       // Convert the current count of line numbers      
       lineNumber = number.str();
       number.str("");
       // If printNumbers is true append the line number before the actual line      
       if(printNumbers){
           out<<lineNumber;
           spaceCount = fieldSize-lineNumber.length();
           for(int i=0;i<spaceCount;i++)
               out<<" ";
       }
       // Print the line to the output stream      
       out<<line<<endl;
       count++;
      
       // If lineSpaces in more than one, out
       if(lineSpaces>1){
           for(int i=1;i<lineSpaces;i++)
               out<<endl;
       }
   }
}
int main()
{
   fstream in; //input file
   in.open("input.txt");
  
   fstream out; //output file
   out.open("output.txt");
  
   fscopy(in, out, true, 3);
  
   return 0;
}

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

Sample Output:

input.txt

Chevrolet.... 23 750
Zapata.Co.... 53 375
Schlumberger. 51 1204
FMC.......... 13 675
Buick........ 22 475
Action.Inc... 41 1712
IBM.......... 32 979
Apple.Comp... 42 790
Zapata.Co.... 52 443
Univ..Mich... 12 1960

output.txt:

1 Chevrolet.... 23 750


2 Zapata.Co.... 53 375


3 Schlumberger. 51 1204


4 FMC.......... 13 675


5 Buick........ 22 475


6 Action.Inc... 41 1712


7 IBM.......... 32 979


8 Apple.Comp... 42 790


9 Zapata.Co.... 52 443


10 Univ..Mich... 12 1960