question related to passing variable number of arguments

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Sep 19, 2007.

  1. Guest

    Hi,

    I want to define a function log_file(), that would print the
    formatted message to stdout if the FILE pointer passed is NULL else
    print the formatted output to the file corresponding to FILE*.

    For eg.

    extern FILE *fp;

    int main(void)
    {
    int n1=0;
    int n2=100;

    log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
    }

    I want that, log_file() should print the formatted output to stdout,
    in case fp is NULL, else
    print the output to the corresponding file.
    Can some one please help ? I tried my best, but could not find any
    solution.

    thanks a lot for any help.
     
    , Sep 19, 2007
    #1
    1. Advertising

  2. jaysome Guest

    On Wed, 19 Sep 2007 00:55:35 -0700, ""
    <> wrote:

    >Hi,
    >
    > I want to define a function log_file(), that would print the
    >formatted message to stdout if the FILE pointer passed is NULL else
    >print the formatted output to the file corresponding to FILE*.
    >
    >For eg.
    >
    >extern FILE *fp;
    >
    >int main(void)
    >{
    > int n1=0;
    > int n2=100;
    >
    > log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
    >}
    >
    >I want that, log_file() should print the formatted output to stdout,
    >in case fp is NULL, else
    >print the output to the corresponding file.
    >Can some one please help ? I tried my best, but could not find any
    >solution.
    >
    >thanks a lot for any help.


    Hi junky fellow,

    The following doesn't deal with your extern fp, because it represents
    a single translation unit that compiles and links. Nevertheless, I
    hope it gives you an idea of how you can solve your problem.

    #include <stdarg.h>
    #include <stdio.h>

    #define MAX_MSG_LENGTH 256 /*or whatever size you need*/

    static FILE *G_fp = NULL;
    static void log_file(FILE *fp, const char *format, ...);

    int main(void)
    {
    int n1 = 0;
    int n2 = 100;

    log_file(G_fp, "SOME MESSAGE n1 = %d and n2 = %d\n", n1, n2);
    return 0;
    }

    static void log_file(FILE *fp, const char *format, ...)
    {
    va_list arg_ptr;
    char text[MAX_MSG_LENGTH + 1];

    va_start(arg_ptr, format);
    vsprintf(text, format, arg_ptr);
    va_end(arg_ptr);
    if ( fp )
    {
    fprintf(fp, "%s", text);
    }
    else
    {
    fprintf(stdout, "%s", text);
    }
    }

    Regards
    --
    jay
     
    jaysome, Sep 19, 2007
    #2
    1. Advertising

  3. Guest

    On Sep 19, 1:35 pm, jaysome <> wrote:
    > On Wed, 19 Sep 2007 00:55:35 -0700, ""
    >
    >
    >
    >
    >
    > <> wrote:
    > >Hi,

    >
    > > I want to define a function log_file(), that would print the
    > >formatted message to stdout if the FILE pointer passed is NULL else
    > >print the formatted output to the file corresponding to FILE*.

    >
    > >For eg.

    >
    > >extern FILE *fp;

    >
    > >int main(void)
    > >{
    > > int n1=0;
    > > int n2=100;

    >
    > > log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
    > >}

    >
    > >I want that, log_file() should print the formatted output to stdout,
    > >in case fp is NULL, else
    > >print the output to the corresponding file.
    > >Can some one please help ? I tried my best, but could not find any
    > >solution.

    >
    > >thanks a lot for any help.

    >
    > Hi junky fellow,
    >
    > The following doesn't deal with your extern fp, because it represents
    > a single translation unit that compiles and links. Nevertheless, I
    > hope it gives you an idea of how you can solve your problem.
    >
    > #include <stdarg.h>
    > #include <stdio.h>
    >
    > #define MAX_MSG_LENGTH 256 /*or whatever size you need*/
    >
    > static FILE *G_fp = NULL;
    > static void log_file(FILE *fp, const char *format, ...);
    >
    > int main(void)
    > {
    > int n1 = 0;
    > int n2 = 100;
    >
    > log_file(G_fp, "SOME MESSAGE n1 = %d and n2 = %d\n", n1, n2);
    > return 0;
    >
    > }
    >
    > static void log_file(FILE *fp, const char *format, ...)
    > {
    > va_list arg_ptr;
    > char text[MAX_MSG_LENGTH + 1];
    >
    > va_start(arg_ptr, format);
    > vsprintf(text, format, arg_ptr);
    > va_end(arg_ptr);
    > if ( fp )
    > {
    > fprintf(fp, "%s", text);
    > }
    > else
    > {
    > fprintf(stdout, "%s", text);
    > }
    >
    > }
    >
    >


    Thanks a lot, jaysome. This really solved the problem.
     
    , Sep 19, 2007
    #3
  4. On Wed, 19 Sep 2007 00:55:35 -0700, ""
    <> wrote:

    >Hi,
    >
    > I want to define a function log_file(), that would print the
    >formatted message to stdout if the FILE pointer passed is NULL else
    >print the formatted output to the file corresponding to FILE*.
    >
    >For eg.
    >
    >extern FILE *fp;
    >
    >int main(void)
    >{
    > int n1=0;
    > int n2=100;
    >
    > log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
    >}
    >
    >I want that, log_file() should print the formatted output to stdout,
    >in case fp is NULL, else
    >print the output to the corresponding file.
    >Can some one please help ? I tried my best, but could not find any
    >solution.


    int logfile(FILE *f, ...){
    /* after processing argument list */
    if (f == NULL) f = stdout;
    return fprintf(...);}

    The real issue is what you are going to do with the format string and
    the subsequent arguments. Do you really intend to generate your own
    code to duplicate the functionality of the *printf functions?

    You could make life easy on yourself and use sprintf to perform all
    the hard work and then use the much simpler

    int logfile(FILE *f, const char *s){
    if (f == NULL) f = stdout;
    return fputs(s, f);}

    At this point, logfile would hardly merit a function and could be
    replaced by a macro.


    Remove del for email
     
    Barry Schwarz, Sep 19, 2007
    #4
  5. CBFalconer Guest

    "" wrote:
    >

    .... snip ...
    >
    > extern FILE *fp;
    >
    > int main(void) {
    > int n1=0;
    > int n2=100;
    >
    > log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
    > }
    >
    > I want that, log_file() should print the formatted output to stdout,
    > in case fp is NULL, else print the output to the corresponding file.
    > Can some one please help ? I tried my best, but could not find any
    > solution.


    In log_file():

    if (NULL == fp) fp = stdout;
    ....

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 19, 2007
    #5
  6. Eric Sosman Guest

    jaysome wrote:
    > On Wed, 19 Sep 2007 00:55:35 -0700, ""
    > <> wrote:
    >
    >> Hi,
    >>
    >> I want to define a function log_file(), that would print the
    >> formatted message to stdout if the FILE pointer passed is NULL else
    >> print the formatted output to the file corresponding to FILE*.
    >>
    >> For eg.
    >>
    >> extern FILE *fp;
    >>
    >> int main(void)
    >> {
    >> int n1=0;
    >> int n2=100;
    >>
    >> log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
    >> }
    >>
    >> I want that, log_file() should print the formatted output to stdout,
    >> in case fp is NULL, else
    >> print the output to the corresponding file.
    >> Can some one please help ? I tried my best, but could not find any
    >> solution.
    >>
    >> thanks a lot for any help.

    >
    > Hi junky fellow,
    >
    > The following doesn't deal with your extern fp, because it represents
    > a single translation unit that compiles and links. Nevertheless, I
    > hope it gives you an idea of how you can solve your problem.
    >
    > #include <stdarg.h>
    > #include <stdio.h>
    >
    > #define MAX_MSG_LENGTH 256 /*or whatever size you need*/
    >
    > static FILE *G_fp = NULL;
    > static void log_file(FILE *fp, const char *format, ...);
    >
    > int main(void)
    > {
    > int n1 = 0;
    > int n2 = 100;
    >
    > log_file(G_fp, "SOME MESSAGE n1 = %d and n2 = %d\n", n1, n2);
    > return 0;
    > }
    >
    > static void log_file(FILE *fp, const char *format, ...)
    > {
    > va_list arg_ptr;
    > char text[MAX_MSG_LENGTH + 1];
    >
    > va_start(arg_ptr, format);
    > vsprintf(text, format, arg_ptr);
    > va_end(arg_ptr);
    > if ( fp )
    > {
    > fprintf(fp, "%s", text);
    > }
    > else
    > {
    > fprintf(stdout, "%s", text);
    > }
    > }


    Ugh. Ugh, ugh, ugh. Use vfprintf() and get rid of
    the perilous fixed-size buffer:

    #include <stdio.h>
    #include <stdarg.h>

    static /* depending on circumstances */
    void log_file(FILE *fp, const char *format, ...)
    {
    va_list arg_ptr;
    va_start(arg_ptr, format);
    vfprintf (fp == NULL ? stdout : fp, format, arg_ptr);
    va_end(arg_ptr);
    }

    --
    Eric Sosman
    lid
     
    Eric Sosman, Sep 19, 2007
    #6
  7. <> wrote in message
    news:...
    > Hi,
    >
    > I want to define a function log_file(), that would print the
    > formatted message to stdout if the FILE pointer passed is NULL else
    > print the formatted output to the file corresponding to FILE*.
    >
    > For eg.
    >
    > extern FILE *fp;
    >
    > int main(void)
    > {
    > int n1=0;
    > int n2=100;
    >
    > log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
    > }
    >
    > I want that, log_file() should print the formatted output to stdout,
    > in case fp is NULL, else
    > print the output to the corresponding file.
    > Can some one please help ? I tried my best, but could not find any
    > solution.
    >
    > thanks a lot for any help.
    >


    Make it real simple:

    #define OUTFILE(f) ((f)?(f):stdout)

    then to log:
    fprintf( OUTFILE(fp), format, <arguments> );

    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Aero Stability and Controls Computing
     
    Fred Kleinschmidt, Sep 19, 2007
    #7
  8. Tor Rustad Guest

    jaysome wrote:
    > On Wed, 19 Sep 2007 00:55:35 -0700, ""
    > <> wrote:
    >
    >> Hi,
    >>
    >> I want to define a function log_file(), that would print the
    >> formatted message to stdout if the FILE pointer passed is NULL else
    >> print the formatted output to the file corresponding to FILE*.
    >>
    >> For eg.
    >>
    >> extern FILE *fp;
    >>
    >> int main(void)
    >> {
    >> int n1=0;
    >> int n2=100;
    >>
    >> log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
    >> }
    >>
    >> I want that, log_file() should print the formatted output to stdout,
    >> in case fp is NULL, else
    >> print the output to the corresponding file.
    >> Can some one please help ? I tried my best, but could not find any
    >> solution.
    >>
    >> thanks a lot for any help.

    >
    > Hi junky fellow,
    >
    > The following doesn't deal with your extern fp, because it represents
    > a single translation unit that compiles and links. Nevertheless, I
    > hope it gives you an idea of how you can solve your problem.
    >
    > #include <stdarg.h>
    > #include <stdio.h>
    >
    > #define MAX_MSG_LENGTH 256 /*or whatever size you need*/
    >
    > static FILE *G_fp = NULL;
    > static void log_file(FILE *fp, const char *format, ...);
    >
    > int main(void)
    > {
    > int n1 = 0;
    > int n2 = 100;
    >
    > log_file(G_fp, "SOME MESSAGE n1 = %d and n2 = %d\n", n1, n2);
    > return 0;
    > }
    >
    > static void log_file(FILE *fp, const char *format, ...)
    > {
    > va_list arg_ptr;
    > char text[MAX_MSG_LENGTH + 1];
    >
    > va_start(arg_ptr, format);
    > vsprintf(text, format, arg_ptr);


    YABOB... KABOOOM!

    Better:

    static void vfile_log(FILE *flog, const char *message, va_list argp);

    void file_log(FILE *flog, const char *msg, ...)
    {
    va_list argp;

    assert(msg != NULL);

    va_start(argp, msg);
    vfile_log(flog, msg, argp);
    va_end(argp);
    }

    static void vfile_log(FILE *flog, const char *message, va_list argp)
    {
    ....

    if (flog && message)
    {
    (void) vfprintf(flog, message, argp);
    (void) fprintf(flog, "\n");
    fflush(flog);
    }
    ....
    }

    --
    Tor <torust [at] online [dot] no>
     
    Tor Rustad, Sep 19, 2007
    #8
  9. jaysome Guest

    On Wed, 19 Sep 2007 07:44:26 -0400, Eric Sosman
    <> wrote:

    >jaysome wrote:
    >> On Wed, 19 Sep 2007 00:55:35 -0700, ""
    >> <> wrote:
    >>
    >>> Hi,
    >>>
    >>> I want to define a function log_file(), that would print the
    >>> formatted message to stdout if the FILE pointer passed is NULL else
    >>> print the formatted output to the file corresponding to FILE*.
    >>>
    >>> For eg.
    >>>
    >>> extern FILE *fp;
    >>>
    >>> int main(void)
    >>> {
    >>> int n1=0;
    >>> int n2=100;
    >>>
    >>> log_file(fp, "SOME MESSAGE n1=%d n2=%d\n", n1, n2);
    >>> }
    >>>
    >>> I want that, log_file() should print the formatted output to stdout,
    >>> in case fp is NULL, else
    >>> print the output to the corresponding file.
    >>> Can some one please help ? I tried my best, but could not find any
    >>> solution.
    >>>
    >>> thanks a lot for any help.

    >>
    >> Hi junky fellow,
    >>
    >> The following doesn't deal with your extern fp, because it represents
    >> a single translation unit that compiles and links. Nevertheless, I
    >> hope it gives you an idea of how you can solve your problem.
    >>
    >> #include <stdarg.h>
    >> #include <stdio.h>
    >>
    >> #define MAX_MSG_LENGTH 256 /*or whatever size you need*/
    >>
    >> static FILE *G_fp = NULL;
    >> static void log_file(FILE *fp, const char *format, ...);
    >>
    >> int main(void)
    >> {
    >> int n1 = 0;
    >> int n2 = 100;
    >>
    >> log_file(G_fp, "SOME MESSAGE n1 = %d and n2 = %d\n", n1, n2);
    >> return 0;
    >> }
    >>
    >> static void log_file(FILE *fp, const char *format, ...)
    >> {
    >> va_list arg_ptr;
    >> char text[MAX_MSG_LENGTH + 1];
    >>
    >> va_start(arg_ptr, format);
    >> vsprintf(text, format, arg_ptr);
    >> va_end(arg_ptr);
    >> if ( fp )
    >> {
    >> fprintf(fp, "%s", text);
    >> }
    >> else
    >> {
    >> fprintf(stdout, "%s", text);
    >> }
    >> }

    >
    > Ugh. Ugh, ugh, ugh. Use vfprintf() and get rid of
    >the perilous fixed-size buffer:
    >
    > #include <stdio.h>
    > #include <stdarg.h>
    >
    > static /* depending on circumstances */
    > void log_file(FILE *fp, const char *format, ...)
    > {
    > va_list arg_ptr;
    > va_start(arg_ptr, format);
    > vfprintf (fp == NULL ? stdout : fp, format, arg_ptr);
    > va_end(arg_ptr);
    > }


    Indeed. Using vfprintf is better than using vsprintf in the instant
    case. The OP should heed your advice.

    Thanks
    --
    jay
     
    jaysome, Sep 20, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    522
  2. Ramashish Baranwal

    Passing variable number of named arguments

    Ramashish Baranwal, Dec 27, 2006, in forum: Python
    Replies:
    2
    Views:
    280
    Ramashish Baranwal
    Dec 28, 2006
  3. Replies:
    3
    Views:
    864
  4. Navaneeth
    Replies:
    4
    Views:
    573
    Kenny McCormack
    Nov 20, 2010
  5. oldyork90
    Replies:
    10
    Views:
    369
    Jorge
    Sep 27, 2008
Loading...

Share This Page