开发者

Passing on a char* which contains a path

开发者 https://www.devze.com 2023-01-27 10:00 出处:网络
I\'m trying to pass through several functions a string with a path but the every \'\\\\\' I put in the path becomes a single \'\\\' in the inner function and I can\'t use it this way.

I'm trying to pass through several functions a string with a path but the every '\\' I put in the path becomes a single '\' in the inner function and I can't use it this way.

Is there a way to preserve the "\\" when entering a new func开发者_如何学运维tion?

I'm using C++ on windows.

thanks :)


Be prepared for some obfuscated answer.

The \ is the escape character (you have probably already encountered the \n escape sequence for example), and \\ is the escape sequence that represents a single \ character (in a sense, it can be understood as an escape of the escape character). If you really want to have \\ in your string, you'll have to use \\\\ :

std::cout << "\\\\something\\" << std::endl; /* prints "\\something\" */

Just to provide another example, suppose you'd like to have some " in a string. Writing :

const char *str = "Hello "World"";

will obviously not compile, and you will have to escape the " with a \ :

const char *str = "Hello \"World\"";


In C++0x you will have a raw string literal:

R"(anything can appear here, even " or \\ )"

Where everything between "( and )" is part of the string -- no escaping necessary. In the current standard you can't achieve what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消