output stream

i am making my own c (no c++ material so that it can work with any c-based language) header file and the next thing i want to make for it is an output stream function. i cant find a tutorial online. so if someone could either point me in the right direction or explain to me how to do it, that would be extremely helpful. also im not looking for source code unless its to check my work against.
You mean a << operator? Look at string:

http://www.cplusplus.com/reference/string/operator%3C%3C/
no like int printf(char *fmt, ...)
i already can print variables and strings and have variable list arguements, but i cant find a way to print to the screen w/o printf
If your making a C header why not just use printf?

If your criteria is that it's C only, then printf fits the bill.
because i want to make my own, for the practice and experience.
The only thing I can suggest is maybe looking at the declaration of printf. I believe it hands quite a bit of the work over to vfprintf, though.

Either way, it's probably going to involve a fair bit of trawling.
> because i want to make my own, for the practice and experience.

With the advent of variadic templates, the practice and experience gained in using <cstdarg> would be quite irrelevant to real-life C++ code. The rest of it would still be a good beginner's exercise.

Start with something extremely minimal.
For instance this (just bare %d, %f, %s, %p and nothing else, and no error handling):

1
2
3
4
5
6
// header
#ifdef __cplusplus
    extern "C"
#endif

int minimalist_printf( const char* format, ... ) ;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// implementation
#include <cstdarg>
#include <string>
#include <sstream>
#include <iostream>

extern "C" int minimalist_printf( const char* format, ... )
{
    va_list args ;
    va_start( args, format ) ;

    std::istringstream istm() ;
    std::ostringstream ostm ;

    std::string fmt(format) ;
    std::string::size_type start = 0, pos ;

    while( ( pos = fmt.find( '%', start ) ) != std::string::npos )
    {
       ostm << fmt.substr( start, pos-start ) ;
       if( pos < ( fmt.size() - 1 ) ) switch( fmt[pos+1] )
       {
           case 'd' : { int v = va_arg(args,int) ; ostm << v ; } break ;
           case 'f' : { double v = va_arg(args,double) ; ostm << v ; } break ;
           case 's' : { const char* v = va_arg( args, const char* ) ; ostm << v ; } break ;
           case 'p' : { const void* v = va_arg( args, const void* ) ; ostm << v ; } break ;
           // etc
           default : ; /* error */
       }
       start = pos+2 ;
    }

    va_end( args );
    if( start < fmt.size() ) ostm << fmt.substr(start) ;
    std::cout << ostm.str() ;
    return ostm.str().size() ;
}


Once you have got the minimalist printf() working correctly, add support for for the other format placeholders in small steps, testing each step as you go along.
http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders
Last edited on
thank you
Topic archived. No new replies allowed.