J
John Flynn
hi,
having problems reading from and writing back to the same file.
basically, i want to read lines of text from a file and reverse them and
write them back to the same file.. it has to replace the text its reversing
eg.
the quick brown fox
jumped over the
lazy dog
.....becomes..
xof nworb kciuq eht
eht revo depmuj
god yzal
following is what i've got so far.. its all over the place.. for me its a
lot of trial and error.. hence all the code that i've made into comments
(//)
any help would be greatly appreciated
********************************************************************
#include <iostream>
#include <fstream>
using namespace std;
/***********************
* Main execution
***********************/
int main()//int argc, char* argv[])
{
// if (argc != 2)
{
// cout << "\nUsage: rever file\n\n";
// return 1;
}
// string fileName(argv[1]);
string readLine;
string tmp;
fstream stream;
fstream outStream;
long readPos, writePos;
stream.open("fileName.txt");
//outStream.open("fileName.txt");
//outStream.open("fileName.txt");
if (!stream.is_open())
{
cout << "Unable to open: " << "fileName" << endl;
//return 1;
}
readPos = 0;
writePos = 0;
for (;
{
// writePos = stream.tellp();
//stream.seekg(readPos, ios::beg);
getline(stream, readLine);
readPos = stream.tellg();
for (int i = readLine.length() - 1; i >= 0; i-- )
{
tmp = tmp + readLine;
}
// stream.seekp(writePos, ios::beg);
// outStream << readPos << endl;
cout << readPos << "and "<< writePos;
cin >> tmp;
if (stream.eof()) break;
tmp = "";
}
}
having problems reading from and writing back to the same file.
basically, i want to read lines of text from a file and reverse them and
write them back to the same file.. it has to replace the text its reversing
eg.
the quick brown fox
jumped over the
lazy dog
.....becomes..
xof nworb kciuq eht
eht revo depmuj
god yzal
following is what i've got so far.. its all over the place.. for me its a
lot of trial and error.. hence all the code that i've made into comments
(//)
any help would be greatly appreciated
********************************************************************
#include <iostream>
#include <fstream>
using namespace std;
/***********************
* Main execution
***********************/
int main()//int argc, char* argv[])
{
// if (argc != 2)
{
// cout << "\nUsage: rever file\n\n";
// return 1;
}
// string fileName(argv[1]);
string readLine;
string tmp;
fstream stream;
fstream outStream;
long readPos, writePos;
stream.open("fileName.txt");
//outStream.open("fileName.txt");
//outStream.open("fileName.txt");
if (!stream.is_open())
{
cout << "Unable to open: " << "fileName" << endl;
//return 1;
}
readPos = 0;
writePos = 0;
for (;
{
// writePos = stream.tellp();
//stream.seekg(readPos, ios::beg);
getline(stream, readLine);
readPos = stream.tellg();
for (int i = readLine.length() - 1; i >= 0; i-- )
{
tmp = tmp + readLine;
}
// stream.seekp(writePos, ios::beg);
// outStream << readPos << endl;
cout << readPos << "and "<< writePos;
cin >> tmp;
if (stream.eof()) break;
tmp = "";
}
}