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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. swprintf_s with *wstring (Resolved)

swprintf_s with *wstring (Resolved)

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studiocomdebugging
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.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    Windows XP, Visual Studio 2008, C++

    bool procedure( wstring *target )
    {...
    int wrote_count = swprintf_s(
    text_to_log,
    MAX_SIZE,
    L"Target is %ls",
    target );
    ... }

    The debugger shows text "Messsage" in variable target but text_to_log does not have that text. text_to_log is declared as WCHAR text_to_log[ MAX_SIZE ] where MAX_SIZE is 400. I have tried creating a local varible of type wstring and the text will copy from target to that local string. Using that local string in the printf statement produces the same result. What do I need to change to incorporate the text from target into the text of text_to_log?

    Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

    D G 2 Replies Last reply
    0
    • B bkelly13

      Windows XP, Visual Studio 2008, C++

      bool procedure( wstring *target )
      {...
      int wrote_count = swprintf_s(
      text_to_log,
      MAX_SIZE,
      L"Target is %ls",
      target );
      ... }

      The debugger shows text "Messsage" in variable target but text_to_log does not have that text. text_to_log is declared as WCHAR text_to_log[ MAX_SIZE ] where MAX_SIZE is 400. I have tried creating a local varible of type wstring and the text will copy from target to that local string. Using that local string in the printf statement produces the same result. What do I need to change to incorporate the text from target into the text of text_to_log?

      Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

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

      What is the value of wrote_count? If -1, what is errno set to?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      B 1 Reply Last reply
      0
      • B bkelly13

        Windows XP, Visual Studio 2008, C++

        bool procedure( wstring *target )
        {...
        int wrote_count = swprintf_s(
        text_to_log,
        MAX_SIZE,
        L"Target is %ls",
        target );
        ... }

        The debugger shows text "Messsage" in variable target but text_to_log does not have that text. text_to_log is declared as WCHAR text_to_log[ MAX_SIZE ] where MAX_SIZE is 400. I have tried creating a local varible of type wstring and the text will copy from target to that local string. Using that local string in the printf statement produces the same result. What do I need to change to incorporate the text from target into the text of text_to_log?

        Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

        G Offline
        G Offline
        Graham Breach
        wrote on last edited by
        #3

        Just a guess, but try using target->c_str() in the swprintf_s function call instead of just target. I believe the printf functions expect C-style strings, not C++ string classes.

        B 1 Reply Last reply
        0
        • D David Crow

          What is the value of wrote_count? If -1, what is errno set to?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

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

          wrote_count is 57, I simplified some when writing this post. That value matches what is in the string it writes to. The text from target is simply missing in action.

          Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

          D 1 Reply Last reply
          0
          • G Graham Breach

            Just a guess, but try using target->c_str() in the swprintf_s function call instead of just target. I believe the printf functions expect C-style strings, not C++ string classes.

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

            Just tried that: error 2228, left of .c_str() must have class/struct/union Edit:

            (*target).c_str()

            You got me most of the way there, evidently there is a precedence problem. With the above the text shows up. Thank you for your help.

            Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

            L 1 Reply Last reply
            0
            • B bkelly13

              wrote_count is 57, I simplified some when writing this post. That value matches what is in the string it writes to. The text from target is simply missing in action.

              Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

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

              bkelly13 wrote:

              The text from target is simply missing in action.

              How are you verifying this?

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              1 Reply Last reply
              0
              • B bkelly13

                Just tried that: error 2228, left of .c_str() must have class/struct/union Edit:

                (*target).c_str()

                You got me most of the way there, evidently there is a precedence problem. With the above the text shows up. Thank you for your help.

                Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

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

                Why not just:

                target->c_str()

                ?

                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