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

istream and ostream detail.

P: n/a
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.
Nov 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
maadhuu wrote:
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
Nov 22 '05 #2

P: n/a
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::ofstream& fout) {
char ch;
if (fin.get(ch)) {
print_backwards(fin, fout);
fout << ch;
}
}

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

Nov 22 '05 #3

P: n/a
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.
meager wroteI 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


Have you thought that this might have been a homework assignment as
well !!

Nov 22 '05 #4

P: n/a
meagar wrote:
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::ofstream& fout) {
char ch;
if (fin.get(ch)) {
print_backwards(fin, fout);
fout << ch;
}
}

int main() {
std::ifstream input('in.txt');
std::ofstream 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
Nov 22 '05 #5

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

Nov 22 '05 #6

P: n/a
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

Nov 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.