replace \ with \\ in string

Hi,

I have a file's location stored in a System::String. E.g.

String^ str = "C:\Test Folder\Test.rtf";

Note, this pathname is actually stored in a text file. I have the code sorted for calling this information and storing it in a string.

I have a richTextBox1 that needs to display the contents of Test.rtf. However, I cannot use richTextBox1->LoadFile(str) because of the slashes being seen as \ not \\.

Is there a quick an easy way to convert this string or to find and replace \ with \\ for the LoadFile() to work?

Cheers,
Last edited on
You shouldn't need to do anything.

\\ is just the way you express the \ character in C/C++. If you specify \, that means expect some special code. For example, \t is tab, \r is carriage return, \n is line feed and \\ is \.

If you read a file that has the string: C:\Temp\names.txt, you can pass that to a file method directly.
Hi kbw,

Thanks for your reply. I understand the idea of escape sequences e.g. \t etc.

And unfortunately, the load file function will only accept \\, not \.

E.g. This works:

richTextBox1->LoadFile("C:\\Test Folder\\Test.rtf");

but this doesn't:

1
2
 String^ str = "C:\Test Folder\Test.rtf";
richTextBox1->LoadFile(str);


nor does this:
richTextBox1->LoadFile("C:\Test Folder\Test.rtf");

Remember, str is actually a line of text from a text file that stores the pathname.

The error message I get is:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

Additional information: Could not find file 'C:\Test Folder\Test.rtf'.


Am I using the LoadFile() incorrectly perhaps?

Cheers,
Last edited on
If you are writing a string literal, '\\' represents a backslash. I don't see why you need to do this programmatically, though. Just use the search & replace function of whatever you favorite editor is to make those changes to the source.
Hi Zhuge,

Thanks for your reply.

Are you suggesting something like this?:

1
2
3
4
5
6
7
8
String^ str = "C:\Test Folder\Test.rtf";
std::size_t found = str.find_first_of("\\");
while (found!=std::string::npos)
{
	str[found] = '\\\\';
	found = str.find_first_of("\\", found + 1);
}
richTextBox1->LoadFile(str);


This doesn't work. I may have the coding wrong from lines 2 to 7 but I'm pretty sure it's because the string is a System::String when I'm finding / replacing using a std::string method.... I'm unsure how to convert a System::String to a std::string (I know how to do it the other way around thought). I assume this could work if I convert from System to std?

Thanks.
That's a different question. You asked about reading content from a file, now you're describing a literal.

You can't do that because String^ str = "C:\Test Folder\Test.rtf"; is incorrect.

The literal must be written as: String^ str = "C:\\Test Folder\\Test.rtf";

I feel like I'm repeating myself here.
Last edited on
Thanks kbw,

Sorry if I haven't been clear. Essentially I'm trying to use a pathname that is stored within a text file which uses single backslashes. I know how to retrieve this and store it as a string (perhaps this approach isn't the right one). I see how the pathname needs to be a literal for the LoadFile() and I'm hoping I'm using the correct terminology here. But the line of text that I'm retrieving has single backslashes. I need to make this pathname so that it uses two.

Some background information that might assist.... I have two programs; one that writes info to a text file, the other reads from this text file. In the first program, there is a button used to ask the user to select a file. It opens a dialog box, the user selects the file (a rtf file in this case) and this pathname is then written to a particular line in the text file. The other program (the one that reads) has a richTextBox. When this program loads the richTextBox displays the rtf file.

I hope this makes it a little clearer.
If you are reading a line of text from a file, as kbw said, it will be fine if you just use it directly. The text file uses backslashes, therefore backslashes will be in the string that holds the data you read.

If you are using a string literal in code, you must escape the backslashes because string literals (not any string, nor string variable, just string literals) use backslashes for special purposes (escape characters).

That's all there is to it.
Thanks Zhuge and kbw,

I get this and this was initial thought but because it wasn't working I thought that there was some other issue. There must be something wrong my code somewhere. I'll investigate...

Cheers.
Hi Zhuge and kwb,

I believe I have solved my problem.

I am using ifstream to open the text file that has the pathname.

I had the code:
richTextBox1->LoadFile(str);

placed before I closed the stream. Placing this line of code after I have closed the file made it work... Not sure if I understand why this is needed but it works. Thanks for your support and getting me to look at my coding again.

Cheers,
Topic archived. No new replies allowed.