Write the definition of a function named fscopy that does a line-by-line copy fr
ID: 3547770 • Letter: W
Question
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 fstreamobjects , 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
int fscopy(fstream &myfile, fstream &outfile, bool numbered, int spacing)
{
int lines=0;
string line;
while(myfile.good())
{
lines++;
getline(myfile, line);
if(numbered == true && line != "" && line != " ")
{
outfile << " " << lines<< " ";
}
outfile << line;
for(int i = 0; i < spacing; i++)
{
outfile << ' ';
}
}
return lines;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.