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

C++ Write the definition of a function named fscopy that does a line-by-line cop

ID: 3816543 • Letter: C

Question

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 bool argument 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

//fcopy function and drive to test it

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

//declare function prototype
int fscopy(ifstream &in, ofstream &out, bool line_no, int line_space);

int main()
{
   ifstream in;
   ofstream out;
   //open input and output file for reading and writing
   in.open("fcopy.txt");
   out.open("fcopyOut.txt");
   if (!in)
   {
       cout << "Not able to open input file fcopy.txt " << endl;
       return -1;
   }
   if (!out)
   {
       cout << "Not able to open input file fcopyOut.txt " << endl;
       return -1;
   }
   //call function
   int linesRead = fscopy(in, out, 0, 0);
   cout << "No of lines read : " << linesRead << endl;
}

int fscopy(ifstream &in, ofstream &out, bool line_no, int line_space)
{
   int lineNo = 1,space,count = 0;
   string str;
   while (!in.eof())
   {
       if (line_no)
       {
           out << lineNo++ << " ";
           getline(in, str);
           out << str << endl;
           count++;
       }
       else
       {
           getline(in, str);
           out << str << endl;
           count++;
       }
       if (line_space)
       {
           space = line_space;
           while (space--)
               out << endl;
       }
   }
   return count;
}

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

//output with line number and space

1 C is a computer programming language developed by Dennis M. Ritchie between 1969 and 1973 at Bell Labs.

2 Both C and C++ will be covered in this course.

3 To become an excellent programmer, the only way is to practice.

//output without line number and space

C is a computer programming language developed by Dennis M. Ritchie between 1969 and 1973 at Bell Labs.
Both C and C++ will be covered in this course.
To become an excellent programmer, the only way is to practice.

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