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. How to copy string with NULL

How to copy string with NULL

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
17 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.
  • C Cedric Moonen

    strncpy will not work: The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. **If count is greater than the length of strSource, the destination string is padded with null characters up to length count**.


    Cédric Moonen Software developer
    Charting control

    N Offline
    N Offline
    Nibu babu thomas
    wrote on last edited by
    #4

    Cedric Moonen wrote:

    strncpy will not work:

    Yeah it won't. Was just a blind hit. Was not sure about it. Read the docs now. Sorry for that. :)


    Nibu thomas A Developer Programming tips[^]  My site[^]

    1 Reply Last reply
    0
    • C color Aljechin

      Hi all, I need to copy a string which may contain NULL characters in between. I may run a loop but I just wanted to know if there is any other way to do this. Normal function like strcpy and all will stop reading after it encounters a NULL since it assumes end of string when it reads NULL. NOTE: I am using plain C. :rose:

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #5

      Aljechin wrote:

      I need to copy a string which may contain NULL characters in between

      How about std::copy[^]?


      My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

      C 1 Reply Last reply
      0
      • N Nibu babu thomas

        Aljechin wrote:

        NOTE: I am using plain C.

        strncpy or memcpy should help.


        Nibu thomas A Developer Programming tips[^]  My site[^]

        C Offline
        C Offline
        color Aljechin
        wrote on last edited by
        #6

        char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest)); Hi, may i please request you to just try this code and comment on why memcpy is failing? Is it the way it goes or am I wrong somewhere? Can you suggest some workaround? Thanks for your time. :rose:

        N D 2 Replies Last reply
        0
        • N Nemanja Trifunovic

          Aljechin wrote:

          I need to copy a string which may contain NULL characters in between

          How about std::copy[^]?


          My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

          C Offline
          C Offline
          color Aljechin
          wrote on last edited by
          #7

          char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest)); I went through that page, but not getting it. Can you show me a small code snippet of how do i copy that szSource into szDest? Sorry if a very elementary question. :rose:

          C N 2 Replies Last reply
          0
          • C color Aljechin

            char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest)); Hi, may i please request you to just try this code and comment on why memcpy is failing? Is it the way it goes or am I wrong somewhere? Can you suggest some workaround? Thanks for your time. :rose:

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #8

            Aljechin wrote:

            Hi, may i please request you to just try this code and comment on why memcpy is failing? Is it the way it goes or am I wrong somewhere? Can you suggest some workaround? Thanks for your time.

            Sure! Please look up memcpy in MSDN. There is a working demo there. See you were saying that szSource contains NULL characters right? So strlen won't work because it will return when it finds the first null character. Same with printf and other functions. They all return when the first NULL character is found. So if your string is...

            "Nibu\0is a\0\0\0good\0boy\0".

            strlen will return 4. printf will print only Nibu


            Nibu thomas A Developer Programming tips[^]  My site[^]

            C 1 Reply Last reply
            0
            • C color Aljechin

              char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest)); I went through that page, but not getting it. Can you show me a small code snippet of how do i copy that szSource into szDest? Sorry if a very elementary question. :rose:

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #9

              What are you trying to do exactly ? Is not because memcpy sucessfully copied the source into the destination that the '\0' character will be removed. It will still be present in the new string so as soon as you try to get its length or print the string, you will only have the begining of the string. This is totally logical.


              Cédric Moonen Software developer
              Charting control

              C 1 Reply Last reply
              0
              • N Nibu babu thomas

                Aljechin wrote:

                Hi, may i please request you to just try this code and comment on why memcpy is failing? Is it the way it goes or am I wrong somewhere? Can you suggest some workaround? Thanks for your time.

                Sure! Please look up memcpy in MSDN. There is a working demo there. See you were saying that szSource contains NULL characters right? So strlen won't work because it will return when it finds the first null character. Same with printf and other functions. They all return when the first NULL character is found. So if your string is...

                "Nibu\0is a\0\0\0good\0boy\0".

                strlen will return 4. printf will print only Nibu


                Nibu thomas A Developer Programming tips[^]  My site[^]

                C Offline
                C Offline
                color Aljechin
                wrote on last edited by
                #10

                Thank you so much. I will surely check that example and get back to you with the problem status. peace homie :rose:

                1 Reply Last reply
                0
                • C Cedric Moonen

                  What are you trying to do exactly ? Is not because memcpy sucessfully copied the source into the destination that the '\0' character will be removed. It will still be present in the new string so as soon as you try to get its length or print the string, you will only have the begining of the string. This is totally logical.


                  Cédric Moonen Software developer
                  Charting control

                  C Offline
                  C Offline
                  color Aljechin
                  wrote on last edited by
                  #11

                  What I am doing? PIP (Peripheral Interface Programming). I have generated a sequence of binary data which will be understood by a peripheral device. For example, we issue a print command from notepad. Its not the .txt file that is sent to the printer by the OS. But a series of printer-understood data is sent. My program generates such device-understandable data which could be just dumped into the port of the device and it will print some meaningful text or act accordingly. I have this binary (kinda junk) stored in unsigned char array. So, I am trying to figure out a way to do this. :) :rose:

                  C 1 Reply Last reply
                  0
                  • C color Aljechin

                    What I am doing? PIP (Peripheral Interface Programming). I have generated a sequence of binary data which will be understood by a peripheral device. For example, we issue a print command from notepad. Its not the .txt file that is sent to the printer by the OS. But a series of printer-understood data is sent. My program generates such device-understandable data which could be just dumped into the port of the device and it will print some meaningful text or act accordingly. I have this binary (kinda junk) stored in unsigned char array. So, I am trying to figure out a way to do this. :) :rose:

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #12

                    So, what is the problem ? Your data is there but if you try to display it with a function that waits for a string, you won't be able to see past the zero char. Just send your buffer and it will work. You need to remember the size of your string of course because strlen won't work.


                    Cédric Moonen Software developer
                    Charting control

                    C 1 Reply Last reply
                    0
                    • C color Aljechin

                      char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest)); I went through that page, but not getting it. Can you show me a small code snippet of how do i copy that szSource into szDest? Sorry if a very elementary question. :rose:

                      N Offline
                      N Offline
                      Nemanja Trifunovic
                      wrote on last edited by
                      #13

                      Aljechin wrote:

                      char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest));

                      char szSource[23] = {"Before\0After"}; char szDest[23]; std::copy(szSource, szSource + 23, szDest); However, as Cedric already noted, don't expect that printf and strlen show the whole thing if you have embedded zeroes.


                      My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                      C 1 Reply Last reply
                      0
                      • C color Aljechin

                        char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest)); Hi, may i please request you to just try this code and comment on why memcpy is failing? Is it the way it goes or am I wrong somewhere? Can you suggest some workaround? Thanks for your time. :rose:

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

                        Aljechin wrote:

                        ptr = memcpy(szDest,szSource,22);

                        Use 12 instead.

                        Aljechin wrote:

                        printf("%s %d",szDest,strlen(szDest));

                        This is highly dependent on szDest being nul-terminated. Using memcpy() implies that you are not treating it as a nul-terminated string.


                        "The largest fire starts but with the smallest spark." - David Crow

                        C 1 Reply Last reply
                        0
                        • D David Crow

                          Aljechin wrote:

                          ptr = memcpy(szDest,szSource,22);

                          Use 12 instead.

                          Aljechin wrote:

                          printf("%s %d",szDest,strlen(szDest));

                          This is highly dependent on szDest being nul-terminated. Using memcpy() implies that you are not treating it as a nul-terminated string.


                          "The largest fire starts but with the smallest spark." - David Crow

                          C Offline
                          C Offline
                          color Aljechin
                          wrote on last edited by
                          #15

                          Thank you so much. :rose:

                          1 Reply Last reply
                          0
                          • N Nemanja Trifunovic

                            Aljechin wrote:

                            char szSource[23] = {"Before\0After"}; char szDest[23]; ptr = memcpy(szDest,szSource,22); printf("%s %d",szDest,strlen(szDest));

                            char szSource[23] = {"Before\0After"}; char szDest[23]; std::copy(szSource, szSource + 23, szDest); However, as Cedric already noted, don't expect that printf and strlen show the whole thing if you have embedded zeroes.


                            My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                            C Offline
                            C Offline
                            color Aljechin
                            wrote on last edited by
                            #16

                            Thank you so much. I will try this. :rose:

                            1 Reply Last reply
                            0
                            • C Cedric Moonen

                              So, what is the problem ? Your data is there but if you try to display it with a function that waits for a string, you won't be able to see past the zero char. Just send your buffer and it will work. You need to remember the size of your string of course because strlen won't work.


                              Cédric Moonen Software developer
                              Charting control

                              C Offline
                              C Offline
                              color Aljechin
                              wrote on last edited by
                              #17

                              Thank you very much

                              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