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 insert a back slash \ in a string

how to insert a back slash \ in a string

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
10 Posts 3 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 Offline
    C Offline
    caykahve
    wrote on last edited by
    #1

    Hi, I get a path like "C:\folder" in my program. And I want to use it in a function which needs "C:\\folder". I wanted to use CString::Insert() function, but I got the error message 'new line in constant' when I use this: "\" How can I insert a \ in a string? Thanks in advance caykahve

    C D 2 Replies Last reply
    0
    • C caykahve

      Hi, I get a path like "C:\folder" in my program. And I want to use it in a function which needs "C:\\folder". I wanted to use CString::Insert() function, but I got the error message 'new line in constant' when I use this: "\" How can I insert a \ in a string? Thanks in advance caykahve

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

      '\' are used for escape characters. If you want that in a string, you need to add another extra \: "C:\\folder". But as I see your error, I think this is due to the fact that you forgot to 'close' your string (you probably forgot the ") Hope this helps.

      C 1 Reply Last reply
      0
      • C Cedric Moonen

        '\' are used for escape characters. If you want that in a string, you need to add another extra \: "C:\\folder". But as I see your error, I think this is due to the fact that you forgot to 'close' your string (you probably forgot the ") Hope this helps.

        C Offline
        C Offline
        caykahve
        wrote on last edited by
        #3

        I don't know how deep the first path exactly. To be more precise, i can add my code here: // Here I have PFPath as "C:\folder1\folder2" int i=0; while( PFPath.Find("\",i)!=-1 ) { PFPath.Insert(i,(CString)".\"); i+=2; } // Here I want to have PFPath as "C:\\folder1\\folder2" :confused:

        C 1 Reply Last reply
        0
        • C caykahve

          I don't know how deep the first path exactly. To be more precise, i can add my code here: // Here I have PFPath as "C:\folder1\folder2" int i=0; while( PFPath.Find("\",i)!=-1 ) { PFPath.Insert(i,(CString)".\"); i+=2; } // Here I want to have PFPath as "C:\\folder1\\folder2" :confused:

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

          Replace all "\" by "\\". In this line: PFPath.Insert(i,(CString)".\"); Why is it a ".\" ?? Why do you insert a '.' ?

          C 1 Reply Last reply
          0
          • C Cedric Moonen

            Replace all "\" by "\\". In this line: PFPath.Insert(i,(CString)".\"); Why is it a ".\" ?? Why do you insert a '.' ?

            C Offline
            C Offline
            caykahve
            wrote on last edited by
            #5

            I already tried replacing the "\" with "\\". the program can be compiled, but the result is erroneous: Then i have at the beginning of the snippet PFPath: "C:\folder1\folder2" and PFPath: "\" at the end of the snippet. I don't have '.' normally, it was "\". It's a typo that occurred during copying here to the messageboard.

            C 1 Reply Last reply
            0
            • C caykahve

              I already tried replacing the "\" with "\\". the program can be compiled, but the result is erroneous: Then i have at the beginning of the snippet PFPath: "C:\folder1\folder2" and PFPath: "\" at the end of the snippet. I don't have '.' normally, it was "\". It's a typo that occurred during copying here to the messageboard.

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

              caykahve wrote: // Here I have PFPath as "C:\folder1\folder2" int i=0; while( PFPath.Find("\",i)!=-1 ) { PFPath.Insert(i,(CString)".\"); i+=2; } // Here I want to have PFPath as "C:\\folder1\\folder2" Ok, another problem is here: the find function returns the starting position of the substring found in the string. So, when you insert the new character, it will insert it at the position 'i' that is left the same (so 0 in the first case). You must take the value returned by Find() and insert the new char there.

              C 1 Reply Last reply
              0
              • C Cedric Moonen

                caykahve wrote: // Here I have PFPath as "C:\folder1\folder2" int i=0; while( PFPath.Find("\",i)!=-1 ) { PFPath.Insert(i,(CString)".\"); i+=2; } // Here I want to have PFPath as "C:\\folder1\\folder2" Ok, another problem is here: the find function returns the starting position of the substring found in the string. So, when you insert the new character, it will insert it at the position 'i' that is left the same (so 0 in the first case). You must take the value returned by Find() and insert the new char there.

                C Offline
                C Offline
                caykahve
                wrote on last edited by
                #7

                OK, you are right. But still the insert function does not work properly. Here is the changed code snippet: int i=0; i = PFPath.Find("\\",i); while( i>-1 ) { CString found; found.Format("Backslash found at index: %d",i); MessageBox(found); PFPath.Insert(i,(CString)"\\"); MessageBox(PFPath); i += 2; i = PFPath.Find("\\",i); } if(i==-1) MessageBox("not found"); MessageBox(PFPath); Output: Backslash found at index: 2 C:\folder1 \ not found \

                1 Reply Last reply
                0
                • C caykahve

                  Hi, I get a path like "C:\folder" in my program. And I want to use it in a function which needs "C:\\folder". I wanted to use CString::Insert() function, but I got the error message 'new line in constant' when I use this: "\" How can I insert a \ in a string? Thanks in advance caykahve

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

                  If C:\folder is something that you typed at a command prompt or in some text box, there is no need to replace one backslash with two. This is user input, which differs from string literals in your code. Whatever function is using the folder name will work correctly.


                  "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                  C 1 Reply Last reply
                  0
                  • D David Crow

                    If C:\folder is something that you typed at a command prompt or in some text box, there is no need to replace one backslash with two. This is user input, which differs from string literals in your code. Whatever function is using the folder name will work correctly.


                    "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                    C Offline
                    C Offline
                    caykahve
                    wrote on last edited by
                    #9

                    C:\folder is actually is not a user input, it is // Get Program Files Path SHGetSpecialFolderPath(NULL,PFPath.GetBuffer(_MAX_PATH), CSIDL_PROGRAM_FILES, FALSE); and it gets "C:\Program Files" exactly. I add something at the end of this string and then i try to use it in CString WholePath = PFPath + (CString)"\\....\\file.exe; SHELLEXECUTEINFO shellInfo; ... shellInfo.lpFile = WholePath; ::ShellExecuteEx(&shellInfo); If I do not use double slash, I get the error: "The program cannot find the file specified" That's why I need double slash. Why is it so hard to insert the required character in to a string I don't understand. :sigh: Is there some other way to obtain "C:\\Program Files"?

                    D 1 Reply Last reply
                    0
                    • C caykahve

                      C:\folder is actually is not a user input, it is // Get Program Files Path SHGetSpecialFolderPath(NULL,PFPath.GetBuffer(_MAX_PATH), CSIDL_PROGRAM_FILES, FALSE); and it gets "C:\Program Files" exactly. I add something at the end of this string and then i try to use it in CString WholePath = PFPath + (CString)"\\....\\file.exe; SHELLEXECUTEINFO shellInfo; ... shellInfo.lpFile = WholePath; ::ShellExecuteEx(&shellInfo); If I do not use double slash, I get the error: "The program cannot find the file specified" That's why I need double slash. Why is it so hard to insert the required character in to a string I don't understand. :sigh: Is there some other way to obtain "C:\\Program Files"?

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

                      You are still have a bit confused. There is no need to change PFPath, and the string literal being appended to it is fine (because it contains the necessary double backslashes). You don't need to insert anything additional. The variable WholePath contains what it needs to. caykahve wrote: If I do not use double slash, I get the error: "The program cannot find the file specified" Rightly so. But the code snippet you've shown does have a double backslash so I don't see the problem. caykahve wrote: Is there some other way to obtain "C:\\Program Files"? Why, when you are already doing it correctly? If you tried to access a file using code like:

                      "c:\program files\some_folder\mydata\file.dat"

                      not only would the compiler complain about illegal escape characters (e.g., \p and \s and \m make no sense, but \f does), it would have interpreted that as

                      "c:rogram filesome_folderydata\file.dat"

                      hence the file not being found.


                      "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                      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