473,394 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

istream and ostream detail.

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
6 2164
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: matthurne | last post by:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a string-like class which stores its data in a low-level way. My class, called Str, uses a char array and length variable. I've...
3
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and...
2
by: tvn007 | last post by:
Please help why this code not compile I would like it to be something like this: istringstream stringstreams(line); stringstreams>>ptrstudent->bypassed>>ptrstudent->name>>ptrstudent->midterm;...
13
by: Peteroid | last post by:
These don't work (I'm using VS C++.NET 2005 Express with clr:/pure syntax): ostream& operator <<( ostream& output, String^ str ) { output << str ; //compile error return output ; } ...
13
by: Gianni Mariani | last post by:
What I would like to do is read bytes from a stream, any number and any time. I would like it to wait until there are any bytes to read. I want the exact same functionality as cstdio's "fread"...
1
by: arnuld | last post by:
i have noticed some people using ostream header alongwith iostream and i have read the reasons on this groups for that inclusion: iostream does not include all of ostream or soemthing like that. ...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
7
by: john | last post by:
I am reading TC++PL3 and on page 644 it is mentioned: "Flushing an istream is done using sync(). This cannot always be done right. For some kinds of streams, we would have to reread characters...
5
by: kamikaze04 | last post by:
Hello. I have a very newbie question about Streams. The situation is that i have a function (that i cannot modify it's definition/call): public void F1(istream & in){ while( ...) { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.