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. 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
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);
}
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 !!
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
Yes, I did. If he uses my example, and the teacher hasn't explicitly
requested a recursive function, he should probably fail it :|
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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;...
|
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 ;
}
...
|
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"...
|
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.
...
|
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 &...
|
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...
|
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( ...) {
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |