Thread: additional arguments

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    additional arguments

    Hi..

    I work on a function that takes a variable number of arguments. Down there is a code I currently have..
    But how should I handle it, if joined data strlen would be more than the current buffer size (so 1023)?

    Code:
    int st(char *text, ... )
    {   
        char buff[1024];
    
        va_list ap;
        va_start(ap, text);
        vsprintf(buff, text, ap);
        va_end( ap );
    
        printf(buff);
    
        return 0;
    }
    Thanks a lot for help

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    vsnprintf

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use two buffers. Translate the next block into one buffer. Count its length. If it will fit, concatenate it onto the big buffer. If not, handle it however you please.



    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    How should I get the len of all args?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by l2u
    But how should I handle it, if joined data strlen would be more than the current buffer size (so 1023)?
    Why use the temporary buffer at all?
    Code:
    int st(const char *format, ... )
    {
       int result;
       va_list ap;
       va_start(ap, format);
       result = vprintf(format, ap);
       va_end( ap );
       return result;
    }
    [edit]Note:
    Code:
    printf(buff);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. Overloading new/delete with additional arguments
    By _Elixia_ in forum C++ Programming
    Replies: 0
    Last Post: 11-09-2004, 06:26 AM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM