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

The following is for a C++ assignment. Ideas for implementing this one: ********

ID: 3904447 • Letter: T

Question

The following is for a C++ assignment.

Ideas for implementing this one:

***************** It is not acceptable if you read the entire file into memory (such as an array or vector) the print out to the output file ******************

- read the last line of input file and print it to the output file:

-- start from the back of the file, such as in.seekg(0, ios::end)

-- use a while loop to move towards the front of the file

-- check each byte to see if a new line char (' ') is present.

-- if a newline char is found, call getline(in, line) where line is a string var. Output line to output file

- loop to move towards to beginning of the input file.

Thank you!

E8.7 Write a program that reads each line in a file, reverses its lines, and writes them to another file. Suppose the user specifies input.txt and output.txt when prompted for the file names, and input.txt contains the lines Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go. After the program is finished, output.txt should contain The lamb was sure to go. And everywhere that Mary went Its fleece was white as snow Mary had a little lamb

Explanation / Answer

#include <iostream>

#include <cstdlib>

#include <fstream>

using namespace std;

//input.txt

//Mary had a little lamb

// Its fleece was white as snow

// And Everywhere taht May went

// The lamb was sure to go

//

// output.txt

// The lamb was sure to go

// And Everywhere taht May went

// Its fleece was white as snow

// Mary had a little lamb

//

// Driver program to test above functions

int main()

{

ifstream in("input.txt");

ofstream out("output.txt");

if(!in) {

cout << "Cannot open input file. ";

return 1;

}

  

if(!out) {

cout << "Cannot open output file to write. ";

return 1;

}

char ch;

long i;

string line;

// go to end of file (less eof char)

in.seekg(0, ios::end);

i = (long) in.tellg(); // see how many bytes in file

i -= 2; // backup before eof

for( ;i>=0; i--) {

in.seekg(i, ios::beg); // the i index starts at last character

in.get(ch);

if(ch==' '){

getline(in, line);

cout << line << endl; // display in terminal

out << line << endl; // write to file

}

}

in.seekg(0, ios::beg); // get the first line

getline(in, line);

cout << line;// display in terminal

out << line ;// dwrite to file

in.close();

  

}