Thread: problems when using sprintf_s

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Question problems when using sprintf_s

    Hi all. I seem to be having a problem here and I was hoping maybe somebody could lead me in the right direction. I am working on an inheritance program for school. Well, my code compiles right and it looks good but I made the mistake of using sprintf_s. So when I go to run it, I get error saying my buffer is too small. I don't want to mess with the buffer and all that if I don't have to. Is there any other alternative? Preferably one that won't change my code completely? It would be awesome if I could just go in and change that one word without having to change everything else.
    Code:
      if (intMinute < 10)
    	{
    		sprintf_s(charBuf, "0%d", intMinute);
    	}
    	else 
    	{
    		sprintf_s(charBuf, "%d", intMinute);
    	}
    	strMinute += charBuf;
    This is the only place I used this in. Please tell me I am not rewritting this whole thing!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You could read the manual page before trying to use a new function.
    sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l (CRT)

    The second parameter is the SIZE OF THE BUFFER, not the format string.

    Did you get any warnings about type conversion when you compiled it?
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    No actually I didn't. I was actually quite surprised that it compiled as easily as it did.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    From your naming conventions, I guess your are using MFC and CString. Your whole code boils down to a single line:

    strMinute.AppendFormat( _T("%02d"), intMinute );

    This will append the formatted string of a two digit (%2d), leading-zero (%02d)) number to your string. Without any _s and char-buffer problems.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps you should increase the warning level settings for your compiler then.
    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.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Another good alternative is Boost.Format. It is standard C++, for when you don't have access to MFC. It is easier and safer than sprintf(_s).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  2. tile colliision detection problems
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 10-23-2002, 09:26 PM
  3. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM
  4. Problems with my RPG app
    By valar_king in forum Game Programming
    Replies: 1
    Last Post: 12-15-2001, 08:07 PM
  5. problems with too many warning messages?
    By Isometric in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 01:23 AM

Tags for this Thread