Macro problem

Discussion in 'C' started by techme, May 22, 2010.

  1. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    I had a situation in a C program, which contained hundreds of calls to
    "fprintf (stdout, ...", where I needed to flush stdout immediately
    after any stream was sent there. More specifically, I needed to add the
    command "fflush(stdout);" after every command that started with:

    fprintf(stdout
    fprintf (stdout
    fprintf( stdout
    fprintf ( stdout
    fprintf ( stdout
    etc.

    I wound up doing this manually (took about 30 minutes), but I later
    thought that I could have defined a macro to take care of this
    substitution. However, I couldn't even begin to think of the definition
    needed to handle every variable length fprintf call.

    Any ideas?

    Thanks.
     
  2. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    No Such Luck wrote:
    I had a situation in a C program, which contained hundreds of calls
    to
    "fprintf (stdout, ...", where I needed to flush stdout immediately
    after any stream was sent there. More specifically, I needed to add
    the
    command "fflush(stdout);" after every command that started with:

    fprintf(stdout

    Are you familiar with variadic macros?

    In C99 you could do something like

    #define print_it(file_ptr, format, ...) do \
    if (fprintf(file_ptr, format, ##__VA_ARGS__ ) == 0)
    fflush(file_ptr);
    /* else... handle the error if you like */
    while(0)
     
  3. techinspiration

    techinspiration New Member

    Joined:
    Feb 14, 2010
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    0
    Try it i have seen it.
     
  4. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    You can also try it;
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice