Please help I'm new to C++
Using stacks Implementation
I should read a sample text file containing:
text file
begining to end
return text
And return a text file reading:
return text
begining to end
test file
This is the sample code to test the implementation:
#include <iostream>
#include <fstream>
#include <string>
#include "stack1.h"
using namespace std
void reverse (ifstream & infile, ofstream & outfile); //function prototype
int main () {
ifstream infile; // declaring a file for input
ofstream outfile; // declaring a file for output
infile.open ("c:\\myInputFile.txt");
if (!infile) { cout << "File could not be opened. Program terminated." << endl;
return 1; // terminates program
}
outfile.open ("c:\\myOutfile.txt");
reverse (infile, outfile);
infile.close();
outfile.close();
return 0;
}
void reverse (ifstream & infile, ofstream & outfile) {
string s;
stack<string> mystack;
getline(infile, s); // try to read a line of text from the file
while (!infile) { // while not end of file
mystack.push(s);
getline(infile, s); // try to read another line
}
while (! mystack.isEmpty() ) {
s = mystack.pop();
outfile << s << endl; // write s to the outfile
}
}