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. WCHAR copy one char at a time

WCHAR copy one char at a time

Scheduled Pinned Locked Moved ATL / WTL / STL
c++questioncsharpvisual-studio
6 Posts 3 Posters 8 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 7, Visual Studio, C++, MFC and non console type applications that have no windows Given: WCHAR destination[ 60 ] WCHAR source[ 60 ] Presume: destination has 20 characters to be retained. What is the best method of copying all the characters that will fit from source into destination? Easy enough for ASCII, but difficult for this WCHAR novice.

    Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

    L J 2 Replies Last reply
    0
    • B bkelly13

      Windows 7, Visual Studio, C++, MFC and non console type applications that have no windows Given: WCHAR destination[ 60 ] WCHAR source[ 60 ] Presume: destination has 20 characters to be retained. What is the best method of copying all the characters that will fit from source into destination? Easy enough for ASCII, but difficult for this WCHAR novice.

      Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

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

      Just use the wcscpy[^] function. There are wide versions of all the character manipulation functions.

      Veni, vidi, abiit domum

      B 1 Reply Last reply
      0
      • L Lost User

        Just use the wcscpy[^] function. There are wide versions of all the character manipulation functions.

        Veni, vidi, abiit domum

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

        Refer back to the OP. The strings have the same maximum length. String destination already has 20 characters so 60 more will not fit. Function wcscpy() has only two arguments, destination and source. We can write the destination in the format: destination[20] to start there, but there is no way to specify that only 39 of the source characters are to be copied. However, having seen that, I then found wcscpy_s() with three arguments. We can specify destination[ 20 ], then specify the second argument would be 40. We can generalize this with a calculated length. But I am unsure about the right functions to use with WCHAR to get the max length and to get the current length. I will try to find that and post again.

        Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

        L 1 Reply Last reply
        0
        • B bkelly13

          Refer back to the OP. The strings have the same maximum length. String destination already has 20 characters so 60 more will not fit. Function wcscpy() has only two arguments, destination and source. We can write the destination in the format: destination[20] to start there, but there is no way to specify that only 39 of the source characters are to be copied. However, having seen that, I then found wcscpy_s() with three arguments. We can specify destination[ 20 ], then specify the second argument would be 40. We can generalize this with a calculated length. But I am unsure about the right functions to use with WCHAR to get the max length and to get the current length. I will try to find that and post again.

          Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

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

          Take a look at http://msdn.microsoft.com/en-us/library/xdsywd25.aspx[^], which allows you to specify the maximum number of characters. As a developer it is quite important to get to know all the CRT functions, and STL classes.

          Veni, vidi, abiit domum

          B 1 Reply Last reply
          0
          • L Lost User

            Take a look at http://msdn.microsoft.com/en-us/library/xdsywd25.aspx[^], which allows you to specify the maximum number of characters. As a developer it is quite important to get to know all the CRT functions, and STL classes.

            Veni, vidi, abiit domum

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

            Yes, you are so right. Found the page, got the function, using it. Thank you for taking the time to reply.

            Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

            1 Reply Last reply
            0
            • B bkelly13

              Windows 7, Visual Studio, C++, MFC and non console type applications that have no windows Given: WCHAR destination[ 60 ] WCHAR source[ 60 ] Presume: destination has 20 characters to be retained. What is the best method of copying all the characters that will fit from source into destination? Easy enough for ASCII, but difficult for this WCHAR novice.

              Thanks for your time If you work with telemetry, please check this bulletin board: http://www.bkelly.ws/irig\_106/

              J Offline
              J Offline
              Jeremy Falcon
              wrote on last edited by
              #6

              Not sure what you mean by 20 characters needing to be retained. No need to walk the string one-by-one...

              wcsncpy_s(destination, 60, source, 20);

              Will take the first 20 characters of source and copy them to destination. But if you must walk it one-by-one are several ways...

              for(i=0; i < 60; i++)
              {
              WCHAR *current = &source + (sizeof(WCHAR) * i); // one way
              destination[i] = *current;

              WCHAR current = source\[i\]; // another way
              destination\[i\] = current;
              

              }

              I'd never declare a variable in a loop like that, so it's just for illustration. Anyway, you can do whatever logic you want to skip the first 20 chars in destination at that point.

              Jeremy Falcon

              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