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. how to truncate a string based on buffer size.

how to truncate a string based on buffer size.

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
10 Posts 4 Posters 1 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.
  • N Offline
    N Offline
    Nandu_77b
    wrote on last edited by
    #1

    Can any one help me to truncate the string based on the buffer size, for the below show scenario. main() { int size = 50; char errmsg [size]; get_last_error( errmsg, size ); cout<<errmsg; } get_last_error( char *errmsg, int bufsize ) { string sErrMsg; if ( sErrMsg.capacity() > bufsize ) { // here i want to truncate the string, so that the size of string will fit into "bufsize". Then copy the string to errmsg // strcpy( errmsg, sErrMsg.c_str() ); // how to truncate the string based on given buffersize // so that we can atlest copy the possible string to errmsg if not the whole string } else { strcpy( errmsg, sErrMsg.c_str() ); *errcode = nErrCode; } }

    D CPalliniC J 3 Replies Last reply
    0
    • N Nandu_77b

      Can any one help me to truncate the string based on the buffer size, for the below show scenario. main() { int size = 50; char errmsg [size]; get_last_error( errmsg, size ); cout<<errmsg; } get_last_error( char *errmsg, int bufsize ) { string sErrMsg; if ( sErrMsg.capacity() > bufsize ) { // here i want to truncate the string, so that the size of string will fit into "bufsize". Then copy the string to errmsg // strcpy( errmsg, sErrMsg.c_str() ); // how to truncate the string based on given buffersize // so that we can atlest copy the possible string to errmsg if not the whole string } else { strcpy( errmsg, sErrMsg.c_str() ); *errcode = nErrCode; } }

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

      Nandu_77b wrote:

      // how to truncate the string based on given buffersize

      How about the substr() method?

      "Love people and use things, not love things and use people." - Unknown

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      N 1 Reply Last reply
      0
      • D David Crow

        Nandu_77b wrote:

        // how to truncate the string based on given buffersize

        How about the substr() method?

        "Love people and use things, not love things and use people." - Unknown

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        N Offline
        N Offline
        Nandu_77b
        wrote on last edited by
        #3

        Nandu_77b wrote: // how to truncate the string based on given buffersize How about the substr() method? substr() this fun will not take buffer size, it takes length of characters. I need to truncate based on buffer size. Please see my code which will give you clear idea. -Nandu

        D 1 Reply Last reply
        0
        • N Nandu_77b

          Can any one help me to truncate the string based on the buffer size, for the below show scenario. main() { int size = 50; char errmsg [size]; get_last_error( errmsg, size ); cout<<errmsg; } get_last_error( char *errmsg, int bufsize ) { string sErrMsg; if ( sErrMsg.capacity() > bufsize ) { // here i want to truncate the string, so that the size of string will fit into "bufsize". Then copy the string to errmsg // strcpy( errmsg, sErrMsg.c_str() ); // how to truncate the string based on given buffersize // so that we can atlest copy the possible string to errmsg if not the whole string } else { strcpy( errmsg, sErrMsg.c_str() ); *errcode = nErrCode; } }

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          What about strncpy [^]? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          N 1 Reply Last reply
          0
          • CPalliniC CPallini

            What about strncpy [^]? :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            N Offline
            N Offline
            Nandu_77b
            wrote on last edited by
            #5

            strncpy() will also take char length not the buffersize. - Nandu

            N CPalliniC 2 Replies Last reply
            0
            • N Nandu_77b

              strncpy() will also take char length not the buffersize. - Nandu

              N Offline
              N Offline
              Nandu_77b
              wrote on last edited by
              #6

              --strncpy() will also take char length not the buffersize. - Nandu

              1 Reply Last reply
              0
              • N Nandu_77b

                strncpy() will also take char length not the buffersize. - Nandu

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Are you aware that, for ANSI strings, the char length and the buffer size (bytes) are basically the same? :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                N 1 Reply Last reply
                0
                • N Nandu_77b

                  Nandu_77b wrote: // how to truncate the string based on given buffersize How about the substr() method? substr() this fun will not take buffer size, it takes length of characters. I need to truncate based on buffer size. Please see my code which will give you clear idea. -Nandu

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

                  If the size of your buffer is 50, then you would extract characters 0-49.

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Are you aware that, for ANSI strings, the char length and the buffer size (bytes) are basically the same? :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    N Offline
                    N Offline
                    Nandu_77b
                    wrote on last edited by
                    #9

                    Yes you are correct, Thanks Nandu

                    1 Reply Last reply
                    0
                    • N Nandu_77b

                      Can any one help me to truncate the string based on the buffer size, for the below show scenario. main() { int size = 50; char errmsg [size]; get_last_error( errmsg, size ); cout<<errmsg; } get_last_error( char *errmsg, int bufsize ) { string sErrMsg; if ( sErrMsg.capacity() > bufsize ) { // here i want to truncate the string, so that the size of string will fit into "bufsize". Then copy the string to errmsg // strcpy( errmsg, sErrMsg.c_str() ); // how to truncate the string based on given buffersize // so that we can atlest copy the possible string to errmsg if not the whole string } else { strcpy( errmsg, sErrMsg.c_str() ); *errcode = nErrCode; } }

                      J Offline
                      J Offline
                      Joe Woodbury
                      wrote on last edited by
                      #10

                      lstrcpynA(errmsg, bufsize); If using TCHAR, do: lstrcpyn(errmsg, bufsize / sizeof(TCHAR)); (lstrcpyn is a tiny bit more efficient than strncpy since it doesn't fill the rest of the buffer with zeros after completing the copy. With large buffers, this is generally a waste of cycles.)

                      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                      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