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. PadRight

PadRight

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsharp
16 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.
  • T Offline
    T Offline
    teejayem
    wrote on last edited by
    #1

    .NET has a function called String.PadRight. how can i duplicate this in win32? thanks in advance,

    Don't be overcome by evil, but overcome evil with good

    M D 2 Replies Last reply
    0
    • T teejayem

      .NET has a function called String.PadRight. how can i duplicate this in win32? thanks in advance,

      Don't be overcome by evil, but overcome evil with good

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Something like this maybe (not tested!)...

      LPTSTR MyPadRight(LPCTSTR pSrcStr, int nNewLen, TCHAR padchar)
      {
         int nNewStrLen = max(_tcslen(pSrcStr) + 1, nNewLen + 1);

      TCHAR *pNewStr = new TCHAR[nNewStrLen];

      _tcscpy(pNewStr, pSrcStr)

      for (int i = _tcslen(pSrcStr); i < nNewStrLen - 1; ++i)
            pNewStr[i] = padchar;

      pNewStr[nNewStrLen - 1] = _T('\0');

      return pNewStr;
      }

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      T 1 Reply Last reply
      0
      • M Mark Salsbery

        Something like this maybe (not tested!)...

        LPTSTR MyPadRight(LPCTSTR pSrcStr, int nNewLen, TCHAR padchar)
        {
           int nNewStrLen = max(_tcslen(pSrcStr) + 1, nNewLen + 1);

        TCHAR *pNewStr = new TCHAR[nNewStrLen];

        _tcscpy(pNewStr, pSrcStr)

        for (int i = _tcslen(pSrcStr); i < nNewStrLen - 1; ++i)
              pNewStr[i] = padchar;

        pNewStr[nNewStrLen - 1] = _T('\0');

        return pNewStr;
        }

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        T Offline
        T Offline
        teejayem
        wrote on last edited by
        #3

        Hey Mark, Thank you for the reply. I set up a similar function to the one you posted but for some reason when i pass the integer looks as if it isn't even initialized. I am passing 15 as the value but when i check it in the debugger it is 13045942:doh:. What can i be doing wrong? the signature for the function is like this:

        static char* PadRight(int nCharsToPad, char *strToPad, char chPadValue);

        and i'm calling it like this:

        StringHelper::PadRight(15, "padMe", '0');

        Thanks again,

        Don't be overcome by evil, but overcome evil with good

        M 1 Reply Last reply
        0
        • T teejayem

          Hey Mark, Thank you for the reply. I set up a similar function to the one you posted but for some reason when i pass the integer looks as if it isn't even initialized. I am passing 15 as the value but when i check it in the debugger it is 13045942:doh:. What can i be doing wrong? the signature for the function is like this:

          static char* PadRight(int nCharsToPad, char *strToPad, char chPadValue);

          and i'm calling it like this:

          StringHelper::PadRight(15, "padMe", '0');

          Thanks again,

          Don't be overcome by evil, but overcome evil with good

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          teejayem wrote:

          I am passing 15 as the value but when i check it in the debugger it is 13045942

          I don't know why that would happen. :confused:

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          T 1 Reply Last reply
          0
          • M Mark Salsbery

            teejayem wrote:

            I am passing 15 as the value but when i check it in the debugger it is 13045942

            I don't know why that would happen. :confused:

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            T Offline
            T Offline
            teejayem
            wrote on last edited by
            #5

            its wierd. everytime it is a different value. if i pass 15 it shows 1314822 in the debugger if i pass 10 it shows 13438982. I'll keep searching, mabye i might find something if i do i'll post it here. Thanks Mark

            Don't be overcome by evil, but overcome evil with good

            M 1 Reply Last reply
            0
            • T teejayem

              its wierd. everytime it is a different value. if i pass 15 it shows 1314822 in the debugger if i pass 10 it shows 13438982. I'll keep searching, mabye i might find something if i do i'll post it here. Thanks Mark

              Don't be overcome by evil, but overcome evil with good

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Without seeing your implementation, I have no idea. How and where in the code are you checking the value? Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              T 1 Reply Last reply
              0
              • M Mark Salsbery

                Without seeing your implementation, I have no idea. How and where in the code are you checking the value? Mark

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                T Offline
                T Offline
                teejayem
                wrote on last edited by
                #7

                below is what i have for the header file. Also, i uploaded a Screenshot[^] of the implementation. i know the padright isn't correct yet but i haven't been able to test it correctly. how i am checking it is by breaking into the debugger and mouseovering the nCharsToPad. Now that i look at it furthur none of the parameters are right. The screenshot shows the values in the watch window.

                namespace Server {

                class StringHelper {
                	public:
                		static char\* PadRight(int nCharsToPad, char \*strToPad, char chPadValue);
                };
                

                }

                Don't be overcome by evil, but overcome evil with good

                M M 2 Replies Last reply
                0
                • T teejayem

                  below is what i have for the header file. Also, i uploaded a Screenshot[^] of the implementation. i know the padright isn't correct yet but i haven't been able to test it correctly. how i am checking it is by breaking into the debugger and mouseovering the nCharsToPad. Now that i look at it furthur none of the parameters are right. The screenshot shows the values in the watch window.

                  namespace Server {

                  class StringHelper {
                  	public:
                  		static char\* PadRight(int nCharsToPad, char \*strToPad, char chPadValue);
                  };
                  

                  }

                  Don't be overcome by evil, but overcome evil with good

                  M Offline
                  M Offline
                  Mattias G
                  wrote on last edited by
                  #8

                  That error looks like a corrupted stack to me. I would check locally declared vars (especially arrays) in the function calling your StringHelper

                  T 1 Reply Last reply
                  0
                  • M Mattias G

                    That error looks like a corrupted stack to me. I would check locally declared vars (especially arrays) in the function calling your StringHelper

                    T Offline
                    T Offline
                    teejayem
                    wrote on last edited by
                    #9

                    i think found out what it was (thanks to the help of you two). When i compiled it in 'release' it was messing all of the variables up when i would check them in the debugger But in debug it would look fine. At that point i knew it would have to of been something in my project settings so i went through and compared everything between 'debug' and 'release'. The culprit is in C/C++ -> Optimization -> Optimization. It was set on 'Maximize Speed (/O2)'. when i set it to 'Disabled /Od' everything looked fine. It seems like the program still works fine when the compiler is set to /O2 you just wont be able to see your variables while your debugging.

                    Don't be overcome by evil, but overcome evil with good

                    1 Reply Last reply
                    0
                    • T teejayem

                      .NET has a function called String.PadRight. how can i duplicate this in win32? thanks in advance,

                      Don't be overcome by evil, but overcome evil with good

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

                      teejayem wrote:

                      how can i duplicate this in win32?

                      With something like:

                      char *padRight( char *pStr, int nLen )
                      {
                      char *pTemp = new char[nLen + 1];

                      sprintf(pTemp, "%-\*s", nLen, pStr);
                      
                      return pTemp;
                      

                      }

                      The calling function would need to delete the memory using the delete[] operator.


                      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

                      1 Reply Last reply
                      0
                      • T teejayem

                        below is what i have for the header file. Also, i uploaded a Screenshot[^] of the implementation. i know the padright isn't correct yet but i haven't been able to test it correctly. how i am checking it is by breaking into the debugger and mouseovering the nCharsToPad. Now that i look at it furthur none of the parameters are right. The screenshot shows the values in the watch window.

                        namespace Server {

                        class StringHelper {
                        	public:
                        		static char\* PadRight(int nCharsToPad, char \*strToPad, char chPadValue);
                        };
                        

                        }

                        Don't be overcome by evil, but overcome evil with good

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #11

                        Also be careful - if your string needs to be NULL-terminated and you're not using an ASCIIZ as a pad character... your function doesn't NULL-terminate the string. Mark

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        T 1 Reply Last reply
                        0
                        • M Mark Salsbery

                          Also be careful - if your string needs to be NULL-terminated and you're not using an ASCIIZ as a pad character... your function doesn't NULL-terminate the string. Mark

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          T Offline
                          T Offline
                          teejayem
                          wrote on last edited by
                          #12

                          Hey Mark, Thanks again for the help. Below is what i have for the padright function (tested it and it seems to work). It might not be the most effecient thing in the world but i'm just a little C# programmer trying to harness the power of C++. I've only been working with Unmanaged C for a week now

                          char* StringHelper::PadRight(int nCharsToPad, char *strToPad, char chPadValue) {

                          char\* strPadResult;
                          int nCurLen = strlen(strToPad);
                          strPadResult = (char\*)malloc(sizeof(nCharsToPad));
                          
                          for (int i = 0; i <= nCharsToPad - nCurLen;i++) {
                          	strPadResult\[i\] = chPadValue;
                          }
                          
                          int nIndex = 0;
                          for (int j = nCharsToPad - nCurLen; j < nCharsToPad; j++) {
                          	strPadResult\[j\] = strToPad\[nIndex\];
                          	nIndex++;
                          }
                          
                          strPadResult\[nCharsToPad\] = '\\0';
                          return strPadResult;
                          

                          }

                          Thanks again!

                          Don't be overcome by evil, but overcome evil with good

                          M 1 Reply Last reply
                          0
                          • T teejayem

                            Hey Mark, Thanks again for the help. Below is what i have for the padright function (tested it and it seems to work). It might not be the most effecient thing in the world but i'm just a little C# programmer trying to harness the power of C++. I've only been working with Unmanaged C for a week now

                            char* StringHelper::PadRight(int nCharsToPad, char *strToPad, char chPadValue) {

                            char\* strPadResult;
                            int nCurLen = strlen(strToPad);
                            strPadResult = (char\*)malloc(sizeof(nCharsToPad));
                            
                            for (int i = 0; i <= nCharsToPad - nCurLen;i++) {
                            	strPadResult\[i\] = chPadValue;
                            }
                            
                            int nIndex = 0;
                            for (int j = nCharsToPad - nCurLen; j < nCharsToPad; j++) {
                            	strPadResult\[j\] = strToPad\[nIndex\];
                            	nIndex++;
                            }
                            
                            strPadResult\[nCharsToPad\] = '\\0';
                            return strPadResult;
                            

                            }

                            Thanks again!

                            Don't be overcome by evil, but overcome evil with good

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #13

                            You're going to have problems there... sizeof(nCharsToPad) equals 4 on a 32-bit build....is that how many bytes you want to allocate? Even if you fix it, you need to add one char for the NULL terminator. nCharsToPad is an ambiguous name to me...is it the number of characters to add to the string or the desired string length? It also looks like you're padding the left and justifying the string to the right.  PadLeft sounds like a more appropriate method name. Mark

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            T 1 Reply Last reply
                            0
                            • M Mark Salsbery

                              You're going to have problems there... sizeof(nCharsToPad) equals 4 on a 32-bit build....is that how many bytes you want to allocate? Even if you fix it, you need to add one char for the NULL terminator. nCharsToPad is an ambiguous name to me...is it the number of characters to add to the string or the desired string length? It also looks like you're padding the left and justifying the string to the right.  PadLeft sounds like a more appropriate method name. Mark

                              Mark Salsbery Microsoft MVP - Visual C++ :java:

                              T Offline
                              T Offline
                              teejayem
                              wrote on last edited by
                              #14

                              i see what your saying. The SizeOf is takeing the size of the actual datatype and not the actual value? to make things correct i should do

                              strPadResult = (char\*)malloc(nCharsToPad + 1);
                              

                              nCharsToPad is the total length of the string after it is padded. I guess it should be changed to make things more clear.

                              Don't be overcome by evil, but overcome evil with good

                              M 1 Reply Last reply
                              0
                              • T teejayem

                                i see what your saying. The SizeOf is takeing the size of the actual datatype and not the actual value? to make things correct i should do

                                strPadResult = (char\*)malloc(nCharsToPad + 1);
                                

                                nCharsToPad is the total length of the string after it is padded. I guess it should be changed to make things more clear.

                                Don't be overcome by evil, but overcome evil with good

                                M Offline
                                M Offline
                                Mark Salsbery
                                wrote on last edited by
                                #15

                                Yes. Also, why did you implement a padleft when you originally asked for a PadRight implementation? :)

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                T 1 Reply Last reply
                                0
                                • M Mark Salsbery

                                  Yes. Also, why did you implement a padleft when you originally asked for a PadRight implementation? :)

                                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                                  T Offline
                                  T Offline
                                  teejayem
                                  wrote on last edited by
                                  #16

                                  haha, sorry about that. I got a little confused;P. I figured Pad'Right' was when you put your pad on the left and push the orignal value to the 'Right'. I knew String.PadLeft or String.PadRight was what i was looking for in c# i just assumed padright was the one. Sorry for the confusion in this thread. Next time i'll be sure to make things more clear in the future!:->

                                  Don't be overcome by evil, but overcome evil with good

                                  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