Use stream instead printf

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
origin of

Use stream instead printf

Post by origin of »

Hi to all, i'm italian, and my english is poor ;D

I'm writing a c++ kernel, with a printf function....but i think that i can use streams, like cout<<etc..
i'm searching for a tutorial or some guides about streams and how write a cout, cin function.....
Anyone can help me ?
viral

Re:Use stream instead printf

Post by viral »

Hello..
For making cout .. I suggest you to make a printf.. it'll help you..
Now if you have a class called Console with a printf function, then you can have an object cout of this class and you can call it as:

Code: Select all

cout.printf("Hello World");
Also you can overload << operator as:

Code: Select all

Console& Console::operator <<(char *string)
{
        printf("%s", string);
   return *this;
}

Console& Console :: operator << (char c)
{
        printf("%c", c);
   return *this;
}
Hope this will help...
origin of

Re:Use stream instead printf

Post by origin of »

ok, thanks... ;D
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Use stream instead printf

Post by Solar »

That is nice if you already have a functional printf. But if you're starting from scratch, doing a kout is so much easier than a printf with its format string parsing...

Have a look at FirstStep. It's a multiboot kernel that does nothing but print a couple of the multiboot data structures to screen using a kout object.
Every good solution is obvious once you've found it.
thumbs

Re:Use stream instead printf

Post by thumbs »

I think it's an excellent idea, and I did something similar myself. However, the approach I took was to add print and println function to my String class which already had all the << operators and hex conversion etc in place. Then I added sentinel object, such as endl. so

cout << "Num is: " << hex(93) << endl;

... is actually a string object that asks the video driver to print itself whenever it sees a endl token.

There are also explicit print and println methods.
Post Reply