setting read and write point

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 = "";
}




}
 
J

Jacques Labuschagne

John said:
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


Far too complex. Why not read it all in and then write it all out in
reverse order?

#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>

int main(){
using namespace std;
istreambuf_iterator<char> first(cin), last;
vector<char> v(first, last);
copy(v.rbegin(), v.rend(), ostreambuf_iterator<char>(cout));
}


Jacques.
 
S

Sam Holden

Far too complex. Why not read it all in and then write it all out in
reverse order?

Because that results in output completely unrelated to the specified
required output?

And ignores the "same file" requirement that seems rather important?

#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>

int main(){
using namespace std;
istreambuf_iterator<char> first(cin), last;
vector<char> v(first, last);
copy(v.rbegin(), v.rend(), ostreambuf_iterator<char>(cout));
}

#include <cstdio>
#include <stack>

using namespace std;

void in_place_line_reverse(FILE *file) {
stack<int> line;
int c;
fpos_t start_of_line;
fpos_t end_of_line;

fgetpos(file, &start_of_line);
while ((c=getc(file)) != EOF) {
if (c=='\n') {
fgetpos(file, &end_of_line);
fsetpos(file, &start_of_line);
while (line.size() > 0) {
putc(line.top(), file);
line.pop();
}
fsetpos(file, &end_of_line);
fgetpos(file, &start_of_line);
} else {
line.push(c);
}
}
}

int main() {
FILE *file;
file = fopen("text.txt", "r+");
in_place_line_reverse(file);
}


But it's more C than C++ :)

And needs to double in length in order to actually perform
error checking on all those functions that can fail :(
 
K

Kai-Uwe Bux

Sam said:
Because that results in output completely unrelated to the specified
required output?

And ignores the "same file" requirement that seems rather important?



#include <cstdio>
#include <stack>

using namespace std;

void in_place_line_reverse(FILE *file) {
stack<int> line;
int c;
fpos_t start_of_line;
fpos_t end_of_line;

fgetpos(file, &start_of_line);
while ((c=getc(file)) != EOF) {
if (c=='\n') {
fgetpos(file, &end_of_line);
fsetpos(file, &start_of_line);
while (line.size() > 0) {
putc(line.top(), file);
line.pop();
}
fsetpos(file, &end_of_line);
fgetpos(file, &start_of_line);
} else {
line.push(c);
}
}
}

int main() {
FILE *file;
file = fopen("text.txt", "r+");
in_place_line_reverse(file);
}


But it's more C than C++ :)

And needs to double in length in order to actually perform
error checking on all those functions that can fail :(

What about:

#include <vector>
#include <string>
#include <fstream>
#include <iterator>
#include <algorithm>

typedef std::string Line;
typedef std::vector<Line> Text;

int main( int argn, char ** args ){
Text text;
{ // read:
std::fstream in_file ( args[1] );
Line line;
while ( std::getline( in_file, line ) ) {
text.push_back( line );
}
in_file.close();
}
{ // write:
std::fstream out_file ( args[1] );
std::eek:streambuf_iterator<char> out_iter ( out_file );
for ( Text::const_iterator line_iter = text.begin();
line_iter != text.end(); ++ line_iter ) {
std::copy(line_iter->rbegin(), line_iter->rend(), out_iter );
*out_iter = '\n';
++ out_iter;
}
}
}


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top