How to write a function to wrap snprintf?

Discussion in 'C Programming' started by miloody, Feb 6, 2011.

  1. miloody

    miloody Guest

    Dear all:
    I kept a buffer to handle the strings that output by snprintf and
    tried to write a function to let the buffer works like a ring buffer.
    That means the data will put at the head of buffer when buffer
    overflow.
    I think the prototype of SnPrintBuffer is correct but I have no idea
    what should I pass to snprintf.
    "snprintf (point, var1,fmt);" should be wrong.
    If anyone knows the correct parameter to pass to snprintf, please let
    me know.
    Appreciate your help,
    miloody

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    static unsigned int var1;
    static unsigned int index2;
    static char * point;
    void SnPrintBuffer(const char *fmt, ...)
    {
    index2 = snprintf (point, var1,fmt);
    var1 -= index2;
    point += index2;

    if(var1 < 2)
    { //handle overflow
    }
    }

    int main(void)
    {
    point = malloc(4096);
    var1 = 4096;
    SnPrintBuffer("test 123 %d\n",__LINE__);
    printf("%s",point);
    }
    miloody, Feb 6, 2011
    #1
    1. Advertising

  2. miloody

    Eric Sosman Guest

    On 2/6/2011 9:10 AM, miloody wrote:
    > [...]
    > I think the prototype of SnPrintBuffer is correct but I have no idea
    > what should I pass to snprintf.
    > "snprintf (point, var1,fmt);" should be wrong.
    > If anyone knows the correct parameter to pass to snprintf, please let
    > me know.


    Use va_start() in SnPrintBuffer(), pass the va_list object to
    vsnprintf() (note the "v"), and call va_end() when it returns.

    --
    Eric Sosman
    lid
    Eric Sosman, Feb 6, 2011
    #2
    1. Advertising

  3. miloody

    miloody Guest

    hi

    On Feb 6, 11:08 pm, Eric Sosman <> wrote:
    > On 2/6/2011 9:10 AM, miloody wrote:
    >
    > > [...]
    > > I think the prototype of SnPrintBuffer is correct but I have no idea
    > > what should I pass to snprintf.
    > > "snprintf (point, var1,fmt);" should be wrong.
    > > If anyone knows the correct parameter to pass to snprintf, please let
    > > me know.

    >
    >      Use va_start() in SnPrintBuffer(), pass the va_list object to
    > vsnprintf() (note the "v"), and call va_end() when it returns.

    Is it possible use some ways like the Marco that directly pass the
    parameter from SnPrintBuffer without using va_start() ?
    thanks for your help,
    miloody
    miloody, Feb 6, 2011
    #3
  4. miloody

    Thad Smith Guest

    On 2/6/2011 8:13 AM, miloody wrote:
    > hi
    >
    > On Feb 6, 11:08 pm, Eric Sosman<> wrote:
    >> On 2/6/2011 9:10 AM, miloody wrote:
    >>
    >>> [...]
    >>> I think the prototype of SnPrintBuffer is correct but I have no idea
    >>> what should I pass to snprintf.
    >>> "snprintf (point, var1,fmt);" should be wrong.
    >>> If anyone knows the correct parameter to pass to snprintf, please let
    >>> me know.

    >>
    >> Use va_start() in SnPrintBuffer(), pass the va_list object to
    >> vsnprintf() (note the "v"), and call va_end() when it returns.

    > Is it possible use some ways like the Marco that directly pass the
    > parameter from SnPrintBuffer without using va_start() ?
    > thanks for your help,
    > miloody


    No, macros don't help. Eric's method is the way provided to wrap functions
    taking a variable number of arguments.

    --
    Thad
    Thad Smith, Feb 6, 2011
    #4
  5. On Feb 6, 5:56 pm, Thad Smith <> wrote:
    >
    > No, macros don't help.  Eric's method is the way provided to wrap functions
    > taking a variable number of arguments.
    >

    Generally C allows you to get under the bonnet and do almost anything.
    Constructing variable argument lists on the fly is an exception.
    Malcolm McLean, Feb 6, 2011
    #5
  6. In article <>,
    Malcolm McLean <> wrote:
    >On Feb 6, 5:56 pm, Thad Smith <> wrote:
    >>
    >> No, macros don't help.  Eric's method is the way provided to wrap functions
    >> taking a variable number of arguments.
    >>

    >Generally C allows you to get under the bonnet and do almost anything.
    >Constructing variable argument lists on the fly is an exception.


    That used to be true - until the "what's in the standard and only what's
    in the standard" Nazis took over this newsgroup. Now, all of the fun
    stuff - the stuff that attracted us to C in the first place - is out of
    bounds.

    FWIW, you can certainly construct variable arg lists if you are willing
    to go outside of the standard - Google for "avcall".

    --
    "We should always be disposed to believe that which appears to us to be
    white is really black, if the hierarchy of the church so decides."

    - Saint Ignatius Loyola (1491-1556) Founder of the Jesuit Order -
    Kenny McCormack, Feb 6, 2011
    #6
    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. Chester

    snprintf() for embedded

    Chester, Apr 17, 2004, in forum: C Programming
    Replies:
    1
    Views:
    428
    CBFalconer
    Apr 17, 2004
  2. ajm

    snprintf vs strcat,

    ajm, Jan 14, 2005, in forum: C Programming
    Replies:
    3
    Views:
    572
  3. Aaron Fude

    To wrap or not to wrap?

    Aaron Fude, May 8, 2008, in forum: Java
    Replies:
    12
    Views:
    680
    Chronic Philharmonic
    May 10, 2008
  4. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 22, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    214
    Art Werschulz
    Sep 22, 2003
  5. Art Werschulz

    Text::Wrap::wrap difference

    Art Werschulz, Sep 24, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    229
    Anno Siegel
    Sep 25, 2003
Loading...

Share This Page