Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » printing out a stream

This thread is locked; no one can reply to it. rss feed Print
printing out a stream
William Labbett
Member #4,486
March 2004
avatar

I've got a class with a pointer to an ofstream in it.
I'd like to pass the class to a function so that I can print out the contents of the
ofstream to the screen. I'm really unexpereienced with C++. I'm not sure what'd be best to do. Do I need to close it and reload as a different kind of stream?

// EDIT or RingTFM?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

William - typically you use the ostream to print the class, not the class to print the ostream.

Generally, you use a friend function and overload the stream operator for your class to an ostream&, like this :

class Printer {
protected :
   int x,y;
public :
   /// Declare the operator inside the class as a friend of the class
   friend ostream& operator<<(ostream& os , const Printer& p);
};

/// Define the operator in a source file
ostream& operator<<(ostream& os , const Printer& p) {
   os << "p.x = " << p.x << " , p.y = " << p.y << std::endl;
   return os;
}

Then you use it like this :

Printer p;
cout << p;

Dizzy Egg
Member #10,824
March 2009
avatar

ofstream is a file stream, isn’t it? You’ll need to use that file stream pointer to open a file, read some stuff from it and print it out, unless I’m reading your post wrong.

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

OK, i didn't read too closely. An 'ofstream' by definition goes to an Output File stream, not the screen. You can use std::cout to print to the terminal instead.

Rather if you've written to a file with an ofstream, and you want to read it back, then you need either an fstream, or an ifstream to read it back.

Peter Hull
Member #1,136
March 2001

Maybe you could say a bit more about what you want to do.

If you want to generate some text by streaming data and then do something with it straight away, you could use ostringstream instead of ofstream. Use the << to put stuff into it, then use str() to get it out as a string.

If you really want an object you can stream into, and which outputs to something not the console or a file or a string, then it's not difficult but it is a bit more involved. You need to subclass basic_streambuf - I can give more detail if you want.

William Labbett
Member #4,486
March 2004
avatar

Thanks very much everyone. @Dizzy : you read my post right.

I've got some options now and I think I can do what I wanted to now.

Just to explain what I'm doing :

I wanted to have a text file which opens at the beginning of the program so that if something goes wrong, any would be user can refer to it after the program exits to find out what the problem was. Also, if the user doesn't use the program properly then I need a way to feedback info on why the program can't do what's being asked of it. So I need to use some kind of text output for the info.

The program runs various image manipulation functions for things I can't do in a normal of the shelf art program like PSP.

It's a crazy program, a bit of a diversion from the things I'd like to be able to do if I knew how but it's giving me the right opportunity to practice C++ and learn how to use the allegro5 library. I'm enjoying making it as it's not a big project and it's not a matter of life and death like some of the game projects I've tried to make in the past so it's not stressing me out much. I've got a finite idea of what I want the project to look like finished and my goal is to finish it. It might even help me make graphics for a game when it's done.

Thanks for the info everyone. You've understood what I need to do.

Go to: