有人可以解释C ++转义字符"\"关于Windows文件系统? [英] Could someone explain C++ escape character " \ " in relation to Windows file system?

查看:49
本文介绍了有人可以解释C ++转义字符"\"关于Windows文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的对转义字符"\"及其与Windows文件系统的关系感到困惑.在下面的示例中:

I'm really confused about the escape character " \ " and its relation to the windows file system. In the following example:

char* fwdslash = "c:/myfolder/myfile.txt";
char* backslash = "c:\myfolder\myfile.txt";
char* dblbackslash = "c:\\myfolder\\myfile.txt";

std::ifstream file(fwdslash); // Works
std::ifstream file(dblbackslash); // Works
std::ifstream file(backslash); // Doesn't work

我知道您在这里正在做的操作是转义一个特殊字符,以便您可以在此字符串中使用它.绝对不要通过在字符串常量或std :: string中添加反斜杠来更改字符串---

I get what you are doing here is escaping a special character so you can use it in this string. In no way by placing a backslash in a string literal or std::string do you actually change the string ---

---这是完全错误的,也是我困惑的根源---

--- This is completely wrong, and the source of my confusion---

因此,似乎转义字符仅被某些类或某些东西视为除反斜杠以外的其他含义,例如在控制台上输出,即std :: cout<<\你好";不会打印反斜杠.如果是ifstream(或者我不确定C fopen()版本是否适用),则必须是此类或函数将反斜杠视为转义字符.我想知道,由于Windows文件系统使用反斜杠,因此接受带有反斜杠的简单字符串是否有意义,即"c:\ myfolder \ myfile.txt"?以这种方式尝试失败.

So it seems that the escape character is only treated by certain classes or things to mean something other than a backslash, like outputting on the console, ie., std::cout << "\hello"; will not print the backslash. In the case of ifstream (or I'm not sure if the same applies with the C fopen() version), it must be that this class or function treats backslashes as escape characters. I'm wondering, since the Windows file system uses backslashes wouldn't it make sense for it to accept the simple string with backslashes, ie., "c:\myfolder\myfile.txt" ? Trying it this way fails.

此外,在我的编译器(Visual Studio)中,当包含标头时,可以使用.\和.. \表示当前文件夹或父文件夹.我可以肯定\中的\与转义符无关,但是这些形式是Windows专用的,C预处理程序的一部分还是C或C ++语言的一部分?我知道反斜杠是Windows的事情,因此即使使用.\和.. \

Also, in my compiler (Visual Studio) when I include headers I can use .\ and ..\ to mean either the current folder, or the parent folder. I'm pretty sure the \ in this isn't related to the escape character, but are these forms specific to Windows, part of the C preprocessor, or part of the C or C++ language? I know that backslashes are a Windows thing, so I can't see any reason another system would expect backslashes even when using .\ and ..\

谢谢.

推荐答案

绝对不要在字符串文字中添加反斜杠[...]实际更改字符串

In no way by placing a backslash in a string literal[...] do you actually change the string

您知道.编译器实际上会修改您编写的文字,然后再将其嵌入到已编译的程序中.如果在解析源代码时在字符串或字符常量中发现反斜杠,则会将其忽略,并特别对待下一个字符. \ n 变成回车符,等等.对于没有特殊含义的转义字符,定义了威胁.通常,它只是表示字符不变.

You do. Compiler actually modifies literal you wrote before embedding it into compiled program. If a backslash is found in string or character literal while parsing source code it is ignored and next character is treated specially. \n becomes carriage return, etc. For escaped characters without special meaning threatment is implementation defined. Usually it just means character unchanged.

您不能只传递"c:\ myfolder \ file.txt" ,因为它不是程序将看到的字符串.您的程序将改为显示"c:myfolderfile.txt" .这就是为什么转义的反斜杠具有特殊含义的原因,以允许将反斜杠嵌入到程序将看到的实际字符串中.

You cannot just pass "c:\myfolder\file.txt" because it is not a string which will be seen by your program. Your program will see "c:myfolderfile.txt" instead. This is why escaped backslash has a special meaning, to allow embedding backslashes in actual string your program will see.

解决方案是转义您的反斜杠,或使用原始字符串文字(从C ++ 11开始):

The solution is to either escape your backslashes, or use raw string literals (C++11 onwards):

const char* path = R"(c:\myfolder\file.txt)"

赋予 #include 指令的文件名不是字符串文字,即使它们的格式为"path \ to \ header" ,因此替换规则也不应用于它们

Filenames given to #include directive are not string literals, even if they are in form "path\to\header", so substitution rules are not applied to them.

这篇关于有人可以解释C ++转义字符"\&quot;关于Windows文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆