Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. problem with sprintf_s() on win 7

problem with sprintf_s() on win 7

Scheduled Pinned Locked Moved ATL / WTL / STL
c++csharpvisual-studiodebugginghelp
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 9307625
    wrote on last edited by
    #1

    char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP

    L H 2 Replies Last reply
    0
    • U User 9307625

      char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It looks like you are trying to store a 250 character string into a 50 character buffer, and you have told the formatter that the buffer is only 3 characters long. But ultimately your code makes little sense.

      One of these days I'm going to think of a really clever signature.

      B U 2 Replies Last reply
      0
      • L Lost User

        It looks like you are trying to store a 250 character string into a 50 character buffer, and you have told the formatter that the buffer is only 3 characters long. But ultimately your code makes little sense.

        One of these days I'm going to think of a really clever signature.

        B Offline
        B Offline
        bkelly13
        wrote on last edited by
        #3

        A description I read had GetLastError where I expected WSAGetLastError and my searches did not return those pages. Thanks for the links.

        Thanks for your time

        L 1 Reply Last reply
        0
        • B bkelly13

          A description I read had GetLastError where I expected WSAGetLastError and my searches did not return those pages. Thanks for the links.

          Thanks for your time

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I think you meant to reply on the message thread below.

          One of these days I'm going to think of a really clever signature.

          1 Reply Last reply
          0
          • L Lost User

            It looks like you are trying to store a 250 character string into a 50 character buffer, and you have told the formatter that the buffer is only 3 characters long. But ultimately your code makes little sense.

            One of these days I'm going to think of a really clever signature.

            U Offline
            U Offline
            User 9307625
            wrote on last edited by
            #5

            Even if make 50 char sting into a 50 char buffer, I am getting the same error. Is there any other way to do this? The same code works fine in VS6.0 and not in VS2008.

            L 1 Reply Last reply
            0
            • U User 9307625

              Even if make 50 char sting into a 50 char buffer, I am getting the same error. Is there any other way to do this? The same code works fine in VS6.0 and not in VS2008.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              As I said before, you are specifying a buffer size of 3, so any number of characters greater than that will cause the error. Check the documentation[^] for specific details of the parameters and their values.

              One of these days I'm going to think of a really clever signature.

              1 Reply Last reply
              0
              • U User 9307625

                char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer); When we use the above funcation and application crashed with the folloing message --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: xyz.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line: 244 Expression: ("Buffer too small", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- OS used : Windows 7 32bit OS Microsoft Visual Studio 2008 (VC++) Version 9.0.30729.1SP

                H Offline
                H Offline
                H Brydon
                wrote on last edited by
                #7

                Member 9353776 wrote:

                char buffer[50] int offset =3; char Buffer[250]; sprintf_s(&buffer[offset],offset,"%s",Buffer);

                I am trying to guess what your code is supposed to do here but this might be closer to what you want:

                char buffer[50]
                int offset =3;
                char Buffer[250];

                sprintf_s(&buffer[offset], sizeof(buffer) - offset, "%s", Buffer);

                The second parameter of sprintf_s as used here is the maximum allowed length of the output buffer. If the buffer is of size 50 and you want to write starting in element 3 then the obvious size remaining is 'sizeof(buffer) - offset' and not 'offset'. HTH

                -- Harvey

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups