For this program I am writing, I am suppose to be taking in a text file from the command line, reversing the order of everything in the file and then outputting the text into a new text file with "-reverse" attached on to it. The problem I am having is reversing the order of the lines. I've been able to reverse the text but I need help reversing the lines. I've seen suggestions about using vectors but I'm still new to c++ and I believe i'm not suppose to be using vectors just high-level io For example, filename.txt contains:
abc
edf
dfg
filename-reverse.txt should contain:
gfd
fde
cba
Mine only contains:
cba
fde
gfd
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
/**
* Reverses the line
*/
void reverseStr(string& x)
{
reverse(x.begin(), x.end());
}
int main(int argc, char *argv[])
{
string filename = argv[1];
string filenameCopy = filename;
string reverse = "-reverse";
string reverseFileName;
string reverseLine;
if(filename.rfind(".txt"))//inserts -reverse into existing file name
{
reverseFileName = filenameCopy.insert(filenameCopy.length() - 4,reverse);
}
string line;
ifstream myfile (filename);
ofstream out(reverseFileName);
if (myfile.is_open())
{
/*
vector<string> lines_in_reverse;
while(getline(myfile,line))
{
lines_in_reverse.insert(lines_in_reverse.begin(), line);
}
*/
while(getline(myfile,line))
{
cout << line << endl;
reverseStr(line);
cout << line << endl;
out << line << endl;
}
myfile.close();
}
else
{
cout << "Unable to open file";
}
return EXIT_SUCCESS;
}//main