istream and ostream detail.

M

maadhuu

Hello,
I would like to know as to how you can print a file in the reverse order
,starting from the last character to the 1st .Can I use istream or ostream
classes for this ??Or is there any better way of doing this in C++ ?
Thanking you,
Maadhuu.
 
J

John Harrison

maadhuu said:
Hello,
I would like to know as to how you can print a file in the reverse order
,starting from the last character to the 1st .Can I use istream or ostream
classes for this ??Or is there any better way of doing this in C++ ?
Thanking you,
Maadhuu.

Read the file into memory (say into a std:vector<char>) and then write
it out reversed. You can use istream and ostream for this if you also
use a little imagination.

john
 
M

meagar

I had a question like this once in college, with the arbitrary
requirement that we had to use a recursive function. What surprised me
was how much trouble it gave many people (we were second year at that
point)

#include <iostream>

void print_backwards(std::ifstream& fin, std::eek:fstream& fout) {
char ch;
if (fin.get(ch)) {
print_backwards(fin, fout);
fout << ch;
}
}

int main() {
std::ifstream input('in.txt');
std::eek:fstream output('out.txt');
print_backwards(input, output);
}
 
S

Sandeep

Maadhuu
meager wrote
Have you thought that this might have been a homework assignment as
well !!
 
J

John Harrison

meagar said:
I had a question like this once in college, with the arbitrary
requirement that we had to use a recursive function. What surprised me
was how much trouble it gave many people (we were second year at that
point)

#include <iostream>

void print_backwards(std::ifstream& fin, std::eek:fstream& fout) {
char ch;
if (fin.get(ch)) {
print_backwards(fin, fout);
fout << ch;
}
}

int main() {
std::ifstream input('in.txt');
std::eek:fstream output('out.txt');
print_backwards(input, output);
}

Ouch! Looks like real world considerations didn't count for much on your
course.

Of course the technique above is good for reversing any kind of sequence.

john
 
M

meagar

Yes, I did. If he uses my example, and the teacher hasn't explicitly
requested a recursive function, he should probably fail it :|
 
M

maadhuu

Hello people,
this is not a homework assignment by any means. I had just read about
reversing a file using istream and ostream operators in a book in the
library . I was curious to know how it would work .
Hence i asked this question .
Thanking you
Maadhuu
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top