将字符串转换为原始字符串
char str[] = "C:\Windows\system32"
auto raw_string = convert_to_raw(str);
std::cout << raw_string;
所需的输出:
C:\Windows\system32
有可能吗?我并不是用额外的后挡板使我的小路弦混乱的忠实拥护者。我也不喜欢显式r“()”
符号。
字面上有其他阅读字符串后斜线的工作吗?
char str[] = "C:\Windows\system32"
auto raw_string = convert_to_raw(str);
std::cout << raw_string;
Desired output:
C:\Windows\system32
Is it possible? I am not a big fan of cluttering my path strings with extra backslash. Nor do I like an explicit R"()"
notation.
Any other work-around of reading a backslash in a string literally?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的,
\
在非拉瓦字符串文字中具有特殊的含义,而原始的字符串文字精确存在,以使您有机会避免逃脱东西。放弃,您需要的是r“(...)”
。确实,当您写出
时,您可以验证自己
strlen(str)
is 3,不是4 ,这意味着一旦您编译了该行,在二进制/对象文件中,只有一个字符,newline字符,对应于\ n
;没有\
norn
中没有任何内容,因此您无法检索它们。作为个人品味,我发现原始的弦乐文字很棒!您甚至可以将真实的输入在其中。通常仅以3个字符的价格 -
r
,(
和) - 加上您无论如何都会写的那些。好吧,您必须编写更多字符才能避免任何需求逃脱。查看
从
r
到最后“
)的28个击键,您可以在瞥见中看到它是6行。等效的非RAW字符串
是30个键击,从
> code>。 r
to last“
随附,您必须仔细分析以计数行。一个很短的字符串,您已经看到了优势。
That's not possible,
\
has special meaning inside a non-raw string literal, and raw string literals exist precisely to give you a chance to avoid having to escape stuff. Give up, what you need isR"(...)"
.Indeed, when you write something like
you can verify yourself that
strlen(str)
is 3, not 4, which means that once you compile that line, in the binary/object file there's only one single character, the newline character, corresponding to\n
; there's no\
norn
anywere in it, so there's no way you can retrieve them.As a personal taste, I find raw string literals great! You can even put real Enter in there. Often just for the price of 3 characters -
R
,(
, and)
- in addtion to those you would write anyway. Well, you would have to write more characters to escape anything needs escaping.Look at
That's 28 keystrokes from
R
to last"
included, and you can see in a glimpse it's 6 lines.The equivalent non-raw string
is 30 keystrokes from
R
to last"
included, and you have to parse it carefully to count the lines.A pretty short string, and you already see the advantage.
为了回答问题,不可能是不可能的。
作为不可能的示例,假设我们的路径指定为
“ c:\ a \ b”
;现在,
str
实际上在内存中(在您的程序运行时在程序中)使用静态分配的五个字符的数组,该数组{'c',':':',',','\ 007',' \ 010','\ 000'}
其中'\ xyz'
代表八分代表示(so'\ 010'
是一个char
在十进制中等于8
等于)。问题在于,使用字符串字面的五个字符制作五个字符的数组有多种方法。
在上面,
str1
,str2
,str3,
和str4
均使用五个char 。
这意味着
convert_to_raw(“ c:\ a \ b”)
可以合理地假设它通过了上面的任何字符串,并且可以合法地产生输出
(或其他许多字符串中的任何一个) 。
如果您正在使用Windows路径,则实际问题是
c:\ a \ b
,c:\ 007 \ 010
,c:\ A \ 010
,c:\ 007 \ b
和c:\ x07 \ x08
都是Windows下的有效文件名 - (除非它们是硬链接或连接)命名不同的文件。最后,如果您想在代表文件名或路径的代码中具有字符串文字,则在需要单个后斜线时使用
\\
或RAW String Liralal。或者,由于Windows API功能也接受这些,因此使用所有正向斜线(例如“ c:/a/b”
)将路径作为字符串文字写入代码中。To answer the question, as asked, no it is not possible.
As an example of the impossibility, assume we have a path specified as
"C:\a\b"
;Now,
str
is actually represented in memory (in your program when running) using a statically allocated array of five characters with values{'C', ':', '\007', '\010', '\000'}
where'\xyz'
represents an OCTAL representation (so'\010'
is achar
equal to numerically to8
in decimal).The problem is that there is more than one way to produce that array of five characters using a string literal.
In the above,
str1
,str2
,str3,
andstr4
are all initialised using equivalent arrays of fivechar
.That means
convert_to_raw("C:\a\b")
could quite legitimately assume it is passed ANY of the strings above ANDcould quite legitimately produce output of
(or any one of a number of other strings).
The practical problem with this, if you are working with windows paths, is that
c:\a\b
,C:\007\010
,C:\a\010
,C:\007\b
, andC:\x07\x08
are all valid filenames under windows - that (unless they are hard links or junctions) name DIFFERENT files.In the end, if you want to have string literals in your code representing filenames or paths, then use
\\
or a raw string literal when you need a single backslash. Alternatively, write your paths as string literals in your code using all forward slashes (e.g."C:/a/b"
) since windows API functions accept those too.