how to insert a back slash \ in a string
-
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
-
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
'\' 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.
-
'\' 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.
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: -
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:Replace all "\" by "\\". In this line:
PFPath.Insert(i,(CString)".\");
Why is it a ".\" ?? Why do you insert a '.' ? -
Replace all "\" by "\\". In this line:
PFPath.Insert(i,(CString)".\");
Why is it a ".\" ?? Why do you insert a '.' ?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.
-
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.
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.
-
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.
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 \ -
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
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
-
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:\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 inCString 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"? -
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 inCString 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"?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 variableWholePath
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