sprintf_s in unix?

This is a discussion on sprintf_s in unix? within the C++ Programming forums, part of the General Programming Boards category; Hi all I have a very large program I want to run in my unix computer (Mac) and Windows at ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question sprintf_s in unix?

    Hi all
    I have a very large program I want to run in my unix computer (Mac) and Windows at the same time.
    so I have defined:

    Code:
    #ifndef WIN32
    void sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...) {
    	sprintf(buffer, format, ...); //ERROR: expected primary-expression before '...' token
    }
    #endif
    How can I solve this error?

    I use to have
    Code:
    #define	sprintf_s(buffer, buffer_size, stringbuffer, number) (sprintf(buffer, stringbuffer, number))
    but it does not work with any number of arguments as the real sprintf_s

    Help is appreciated, thanks

    Ignacio
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    sprintf_s is not part of the standard C library so is not portable.

    However, snprintf is, and you should be able to accomplish what you want with that. It's nearly identical, I think.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    Thanks MK27, I completely forgot about snprintf().
    Mac OS 10.6 Snow Leopard : Darwin

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    AFAIK, it does not work correctly in MSVC. The return value etc.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    GCC supports vararg macros.

    Code:
    #define	sprintf_s(buffer, buffer_size, stringbuffer, ...) (sprintf(buffer, stringbuffer, __VA_ARGS__))
    Not 100% sure about the syntax.

    BTW, VC++2005+ does too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  2. Setting up a Unix box
    By @nthony in forum Tech Board
    Replies: 6
    Last Post: 07-22-2007, 11:22 PM
  3. UNIX (Linux, BSD, etc) Programming :: UNIX
    By kuphryn in forum Linux Programming
    Replies: 6
    Last Post: 04-01-2004, 08:44 PM
  4. Unix Sockets
    By prvindia in forum Linux Programming
    Replies: 5
    Last Post: 03-11-2003, 09:16 AM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21