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. Other Discussions
  3. Clever Code
  4. String with \r in the end

String with \r in the end

Scheduled Pinned Locked Moved Clever Code
helpc++debuggingjson
14 Posts 10 Posters 2 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    char test[20];
    strcpy(test, "abcde\r");
    strcat(test, "*");
    printf("%s\n", test);

    printf prints:

    *bcde

    VC++ 2005 debugger shows:

    test "abcde*"

    And actual string is "abcde\r*". I spent two hours fighting with small file parsing error, when every string ended with \r. As usual, looks quite stupid when the problem is solved.

    modified on Monday, September 13, 2010 9:10 AM

    L Z O M B 5 Replies Last reply
    0
    • L Lost User

      char test[20];
      strcpy(test, "abcde\r");
      strcat(test, "*");
      printf("%s\n", test);

      printf prints:

      *bcde

      VC++ 2005 debugger shows:

      test "abcde*"

      And actual string is "abcde\r*". I spent two hours fighting with small file parsing error, when every string ended with \r. As usual, looks quite stupid when the problem is solved.

      modified on Monday, September 13, 2010 9:10 AM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      when fiddling with bytes and characters, I always debug by adding log statements that show the data in hex. I don't want to get fooled by embedded nulls and special characters. And I don't trust Visual Studio debug features much; they tend to "add intelligence" which may work for or against you. And yes, CR (or '\r') can be nasty. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      P 1 Reply Last reply
      0
      • L Lost User

        char test[20];
        strcpy(test, "abcde\r");
        strcat(test, "*");
        printf("%s\n", test);

        printf prints:

        *bcde

        VC++ 2005 debugger shows:

        test "abcde*"

        And actual string is "abcde\r*". I spent two hours fighting with small file parsing error, when every string ended with \r. As usual, looks quite stupid when the problem is solved.

        modified on Monday, September 13, 2010 9:10 AM

        Z Offline
        Z Offline
        zzlove
        wrote on last edited by
        #3

        \r is Carriage return,and it don't equal \n,so you can do this: char test[20]; strcpy(test, "abcde\r\n"); strcat(test, "*"); printf("%s\n", test);

        L 1 Reply Last reply
        0
        • L Luc Pattyn

          when fiddling with bytes and characters, I always debug by adding log statements that show the data in hex. I don't want to get fooled by embedded nulls and special characters. And I don't trust Visual Studio debug features much; they tend to "add intelligence" which may work for or against you. And yes, CR (or '\r') can be nasty. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          P Offline
          P Offline
          PJ Arends
          wrote on last edited by
          #4

          Luc Pattyn wrote:

          when fiddling with bytes and characters, I always debug by adding log statements that show the data in hex

          I prefer to use the memory view windows. It will show the data in hex and ascii. In VS 2005 it is under Debug->Windows->Memory or "Ctrl+Alt+M, 1"


          You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

          1 Reply Last reply
          0
          • Z zzlove

            \r is Carriage return,and it don't equal \n,so you can do this: char test[20]; strcpy(test, "abcde\r\n"); strcat(test, "*"); printf("%s\n", test);

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

            It was file scanning algorithm bug, which added \r to every string by mistake. It would be better to have \r\n in this bug, you are right! I my next bug I will try to do this.

            modified on Thursday, September 16, 2010 10:40 AM

            OriginalGriffO 1 Reply Last reply
            0
            • L Lost User

              char test[20];
              strcpy(test, "abcde\r");
              strcat(test, "*");
              printf("%s\n", test);

              printf prints:

              *bcde

              VC++ 2005 debugger shows:

              test "abcde*"

              And actual string is "abcde\r*". I spent two hours fighting with small file parsing error, when every string ended with \r. As usual, looks quite stupid when the problem is solved.

              modified on Monday, September 13, 2010 9:10 AM

              O Offline
              O Offline
              oggenok64
              wrote on last edited by
              #6

              Now i understand why they call it careless return :-) - turin

              1 Reply Last reply
              0
              • L Lost User

                char test[20];
                strcpy(test, "abcde\r");
                strcat(test, "*");
                printf("%s\n", test);

                printf prints:

                *bcde

                VC++ 2005 debugger shows:

                test "abcde*"

                And actual string is "abcde\r*". I spent two hours fighting with small file parsing error, when every string ended with \r. As usual, looks quite stupid when the problem is solved.

                modified on Monday, September 13, 2010 9:10 AM

                M Offline
                M Offline
                Marc Clifton
                wrote on last edited by
                #7

                Have you ever worked with an old teletype or dotmatrix printer? ;) Marc

                1 Reply Last reply
                0
                • L Lost User

                  char test[20];
                  strcpy(test, "abcde\r");
                  strcat(test, "*");
                  printf("%s\n", test);

                  printf prints:

                  *bcde

                  VC++ 2005 debugger shows:

                  test "abcde*"

                  And actual string is "abcde\r*". I spent two hours fighting with small file parsing error, when every string ended with \r. As usual, looks quite stupid when the problem is solved.

                  modified on Monday, September 13, 2010 9:10 AM

                  B Offline
                  B Offline
                  buaaxiao
                  wrote on last edited by
                  #8

                  char test[20]; strcpy(test, "abcde"); strcat(test, "*"); printf("%s\n", test); now the output is abcde* '\r' means the new line will start at the begin of the same line and the former characters will be covered off

                  L 1 Reply Last reply
                  0
                  • B buaaxiao

                    char test[20]; strcpy(test, "abcde"); strcat(test, "*"); printf("%s\n", test); now the output is abcde* '\r' means the new line will start at the begin of the same line and the former characters will be covered off

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

                    Thanks, man, you really saved me! And your explanation is fantastic!!

                    M 1 Reply Last reply
                    0
                    • L Lost User

                      Thanks, man, you really saved me! And your explanation is fantastic!!

                      M Offline
                      M Offline
                      Matt Gerrans
                      wrote on last edited by
                      #10

                      \r can be nifty for updating status in place in a console tool; like showing a twirling bar, or updating percentage complete value. You can see it in something like pkzip or robocopy. Some of us are even old enough to have written code like this. :-D

                      Matt Gerrans

                      B 1 Reply Last reply
                      0
                      • L Lost User

                        It was file scanning algorithm bug, which added \r to every string by mistake. It would be better to have \r\n in this bug, you are right! I my next bug I will try to do this.

                        modified on Thursday, September 16, 2010 10:40 AM

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        You plan your bugs in advance? Now that's organised! :laugh: Is there a form you have to fill in before you start coding a bug, or do you just wing it and publicise it later? (I used to have a Bug Request form, produced by the same secretary that issued the "Fire in the building instructions": "In case of fire, do not leave the lift.")

                        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        L 1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          You plan your bugs in advance? Now that's organised! :laugh: Is there a form you have to fill in before you start coding a bug, or do you just wing it and publicise it later? (I used to have a Bug Request form, produced by the same secretary that issued the "Fire in the building instructions": "In case of fire, do not leave the lift.")

                          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

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

                          Some programmers don't understand what is Wicked Code forum, and explain me how to fix this bug, something like "Hey, just remove \r in the end, and everything will be fine!". I really don't know how to react to these posts, planning my bugs is kind of sarcasm. But generally, this is a good idea, planned bugs are easy to fix. Is Bug Request form available online and free for commercial use?

                          1 Reply Last reply
                          0
                          • M Matt Gerrans

                            \r can be nifty for updating status in place in a console tool; like showing a twirling bar, or updating percentage complete value. You can see it in something like pkzip or robocopy. Some of us are even old enough to have written code like this. :-D

                            Matt Gerrans

                            B Offline
                            B Offline
                            Blake Miller
                            wrote on last edited by
                            #13

                            And what is really cool is when you pipe the output from a console application like that to a text file.... X|

                            M 1 Reply Last reply
                            0
                            • B Blake Miller

                              And what is really cool is when you pipe the output from a console application like that to a text file.... X|

                              M Offline
                              M Offline
                              Matt Gerrans
                              wrote on last edited by
                              #14

                              Not a problem if the program was well written; for example robocopy has the /np option for exactly that purpose.

                              Matt Gerrans

                              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