problem using sprintf

This is a discussion on problem using sprintf within the C++ Programming forums, part of the General Programming Boards category; Code: char buffer[20]; int integer = 5; sprintf(buffer, "The Number Is: %d", integer); MessageBox(window_handle, "Notice...", buffer, MB_OK); this gives buffer ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    114

    problem using sprintf

    Code:
    char buffer[20]; 
    int integer = 5; 
    sprintf(buffer, "The Number Is: %d", integer); 
    
    MessageBox(window_handle, "Notice...", buffer, MB_OK);
    this gives buffer as "The Number Is: 5"
    but i wanna add another string to the back
    Code:
    sprintf(buffer, "The Number Is: %d", integer ", My additional string");
    so buffer ends up with "The Number Is: 5, My additional string"


    The above doesnt work, why?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    See my answer on Cpp-Home...
    My best code is written with the delete key.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904

    Re: problem using sprintf

    Originally posted by hanhao
    Code:
    sprintf(buffer, "The Number Is: %d", integer ", My additional string");
    so buffer ends up with "The Number Is: 5, My additional string"
    OK, what i'd suggest is if you just want to append another string on to that one, then do something like this:

    Code:
    strcat(buffer,"My additional string");
    however, if you want to place it anywhere in the string (using sprintf), then you can do this:

    Code:
    sprintf(buffer,"The Number Is: %d  %s",integer,myString);
    you could really put that %s anywhere you felt like, just remember the order that you want to place the variables in to the stream, and also make sure that there is enough room in the buffer, or you'll get an overflow and your program will start going crazy

  4. #4
    Registered User deadpoet's Avatar
    Join Date
    Jan 2004
    Posts
    50
    You will also need to ensure that your buffer is large enough to handle that additional character data. You have it set to 20 and this does not appear to be large enough. You can use strlen to get the size of the strings and then do not forget to add 1 for the trailing null character.

    DeadPoet

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. sprintf problem
    By waxby in forum C Programming
    Replies: 4
    Last Post: 01-15-2003, 03:48 PM

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