Page 1 of 1

Use stream instead printf

Posted: Thu Jul 13, 2006 8:13 am
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 ?

Re:Use stream instead printf

Posted: Thu Jul 13, 2006 11:12 am
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...

Re:Use stream instead printf

Posted: Thu Jul 13, 2006 11:36 am
by origin of
ok, thanks... ;D

Re:Use stream instead printf

Posted: Thu Jul 13, 2006 10:29 pm
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.

Re:Use stream instead printf

Posted: Sun Jul 16, 2006 4:54 pm
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.