Thread: sprintf

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    sprintf

    Hello I am just wondering how do I convert my strings to the "sprintf" without any errors or possible losses of data.

    Code:
    TCHAR chText[150];
    	TCHAR chText2[150];
    	TCHAR chLine[300];
    
    	chLine = sprintf(chText, " - ", chText2);
    The error note if it helps: "error C2664: 'sprintf' : cannot convert parameter 2 from 'TCHAR' to 'const char *"

    Thx

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    If you're using potentially wide character strings under Windows you're advised to use wsprintf.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the various manual pages on MSDN tell you how to use TCHAR effectively so that functions which use narrow and wide chars will be used correctly depending on your project settings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    You use sprintf incorrectly. If you want to copy a string, you should use %s.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Try this code:
    Code:
    TCHAR chText[150];
    TCHAR chText2[150];
    TCHAR chLine[300];
    
    strcpy(chLine, chText);
    strcat(chLine, " - ");
    strcat(chLine, chText2);

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I recomend using _snprintf() if using a MS IDE (compiler)

    Code:
    sprintf(chLine,"%s -- %s",chText,chText2);
    But there is no check that the text will fit into the chLine array. If it does not then BANG/SPLAT!

    Code:
    _snprintf(chLine,299,"%s -- %s",chText,chText2);// you can set the max number of characters added to the buffer to avoid buffer over-run
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Actually you really should use a pointer if you have variable text sizes, like this:
    Code:
    TCHAR chText[150];
    TCHAR chText2[150];
    TCHAR *pchLine =NULL;
    
    pchLine = (TCHAR*)malloc(strlen(chText) + strlen(chText2) + 4);
    strcpy(pchLine, chText);
    strcat(pchLine, " - ");
    strcat(pchLine, chText2);
    
    // Do shtufff...
    
    free(pchLine);   // Be sure to clean up.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Actually you really should use ...
    Actually, shouldn't you be using functions other than strcpy, strcat if you are using TCHAR? None of which appear to have been mentioned yet -- except where Salem recommended a tour of the MSDN?
    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.*

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Thank you for helping now the app. works properly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 10:21 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 04:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM