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. C / C++ / MFC
  4. sprintf vs strcpy

sprintf vs strcpy

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studioquestion
7 Posts 5 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.
  • B Offline
    B Offline
    Blake Miller
    wrote on last edited by
    #1

    So I see a lot of code like this lately: sprintf(pUserPwd, "%s", oVerifedWriteDlg.m_UserPwd); Why use that instead of just going with strcpy? strcpy(pUserPwd, oVerifedWriteDlg.m_UserPwd); Am I missing something? I am inclined to think the sprintf would be a lot slower ... :doh:

    D Z L S 4 Replies Last reply
    0
    • B Blake Miller

      So I see a lot of code like this lately: sprintf(pUserPwd, "%s", oVerifedWriteDlg.m_UserPwd); Why use that instead of just going with strcpy? strcpy(pUserPwd, oVerifedWriteDlg.m_UserPwd); Am I missing something? I am inclined to think the sprintf would be a lot slower ... :doh:

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      My personal preference would me to use strcpy(), unless I was constantly changing what I wanted in pUserPwd (e.g., padding, numbers, justification, additional variables).


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      1 Reply Last reply
      0
      • B Blake Miller

        So I see a lot of code like this lately: sprintf(pUserPwd, "%s", oVerifedWriteDlg.m_UserPwd); Why use that instead of just going with strcpy? strcpy(pUserPwd, oVerifedWriteDlg.m_UserPwd); Am I missing something? I am inclined to think the sprintf would be a lot slower ... :doh:

        Z Offline
        Z Offline
        Zac Howland
        wrote on last edited by
        #3

        Laziness is the main culprit there. The sprintf version is far more likely to introduce buffer overrun bugs (e.g. if the wrong delimeter is used). Note that the safer operations are actually to use the std::string class, or at the very least, the strncpy function (or its variants).

        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

        1 Reply Last reply
        0
        • B Blake Miller

          So I see a lot of code like this lately: sprintf(pUserPwd, "%s", oVerifedWriteDlg.m_UserPwd); Why use that instead of just going with strcpy? strcpy(pUserPwd, oVerifedWriteDlg.m_UserPwd); Am I missing something? I am inclined to think the sprintf would be a lot slower ... :doh:

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          If you are writing for Windows only (not cross-platform) you should use the safe string routines strcpy == StringCchCopy[^]

          led mike

          Z 1 Reply Last reply
          0
          • L led mike

            If you are writing for Windows only (not cross-platform) you should use the safe string routines strcpy == StringCchCopy[^]

            led mike

            Z Offline
            Z Offline
            Zac Howland
            wrote on last edited by
            #5

            led mike wrote:

            If you are writing for Windows only (not cross-platform) you should use the safe string routines strcpy == StringCchCopy[^]

            Or the slightly more familiar (syntax-wise) replacements: strXXX_s

            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

            L 1 Reply Last reply
            0
            • Z Zac Howland

              led mike wrote:

              If you are writing for Windows only (not cross-platform) you should use the safe string routines strcpy == StringCchCopy[^]

              Or the slightly more familiar (syntax-wise) replacements: strXXX_s

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              yes they are also safe versions but are not all the same as the strsafe library routines

              You should be aware that strsafe functions, such as StringCchCopy and StringCchCat behave
              differently than strncpy_s and strncat_s. If strncat_s detects an error, the function will
              set the string to NULL. However, by default, strsafe will fill the destination with as much
              data as possible, and then NULL-terminate the string. You can mimic the same behavior in
              strsafe with code like this:

              http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncode/html/secure03102004.asp[^]

              led mike

              1 Reply Last reply
              0
              • B Blake Miller

                So I see a lot of code like this lately: sprintf(pUserPwd, "%s", oVerifedWriteDlg.m_UserPwd); Why use that instead of just going with strcpy? strcpy(pUserPwd, oVerifedWriteDlg.m_UserPwd); Am I missing something? I am inclined to think the sprintf would be a lot slower ... :doh:

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #7

                I'd side step the whole issue and use a string class such as std::string or CString.

                Steve

                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