By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,929 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,929 IT Pros & Developers. It's quick & easy.

setting read and write point

P: n/a
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[i];
}

// stream.seekp(writePos, ios::beg);

// outStream << readPos << endl;

cout << readPos << "and "<< writePos;
cin >> tmp;
if (stream.eof()) break;
tmp = "";
}


}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
John Flynn wrote:
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.
Jul 22 '05 #2

P: n/a
On Thu, 26 Aug 2004 21:28:41 +1200,
Jacques Labuschagne <ja*****@clawshrimp.com> wrote:
John Flynn wrote:
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?


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 :(
--
Sam Holden
Jul 22 '05 #3

P: n/a
Sam Holden wrote:
On Thu, 26 Aug 2004 21:28:41 +1200,
Jacques Labuschagne <ja*****@clawshrimp.com> wrote:
John Flynn wrote:
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?


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 :(


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::ostreambuf_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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.