Skip to main content
Visitor II
January 25, 2008
Question

Redirect stdout in C++

  • January 25, 2008
  • 3 replies
  • 989 views
Posted on January 25, 2008 at 03:43

Redirect stdout in C++

    This topic has been closed for replies.

    3 replies

    robosoftAuthor
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 09:49

    Hi,

    I use the KEIL Realview MDK-ARM

    I want to define a functionpointer in the __FILE struct (stdio.h).

    By changing this functionpointer in my mainprogram I can redirect stdout to some device.

    To do this I changed the file retarget.c to following

     

    #include

     

     

    extern ''C'' {

     

    struct __FILE {

     

    int handle;

     

    int *(*stdoutProc)(int ch); //THIS IS THE FUNCTIONPOINTER I CREATED

     

    };

     

     

    FILE __stdout;

     

    FILE __stdin;

     

     

     

    InitStdOut(int *MyStdOut(int Ch)) {

     

    __stdout.stdoutProc = MyStdOut;

     

    }

     

     

    int fputc (int ch, FILE *f) {

     

    __stdout.stdoutProc(ch);

     

    return(ch);

     

    }

     

    } //extern C

     

    I did not test the code, so maybe the code doesn't do the thing I want to do, but however I can compile it without errors !

    When I rename all the files to *.cpp, to use the compilers C++ extentions, I have the error (at the line FILE __stdout;)

    error #70: incomplete type is not allowed

    How can I add a functionpointer to the __FILE struct in C++ environment ????

    Thank you !

    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 09:49

    You should not modify the struct. To redirect stdout (or stdin, stderr) the standard way is

    console = freopen(''\MYLOGFILE.TXT'',''r+'',stdout); // ''r+'' means read/write

    assuming you have already written the low level drivers for your file system or IO stream.

    robosoftAuthor
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 09:49

    Thanks for response !

    I think its better to write my own printf function.

    Luc