Could someone explain C++ escape character “ \ ” in relation to Windows file system?

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-15 07:18:32

问题


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

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 ---

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

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.

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 ..\

Thanks.


回答1:


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

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.

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.

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

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

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.




回答2:


The single backwards slash practically escapes the next character. In order to get rid of this behavior you need to double escape it. Now for the forward slash, it is probably a compatibility issue which follows the Unix tradition.

Similar thing to this is also in the Java world. A single forward slash is treated for path separation on both Windows and Unix, while also a double backslash.

To make it more clear why single backslash doesn't work, just remember that the following String practically produces a newline, a backslash and a tab:

"\n\\\t"

i.e. in an example like:

""c:\my\next\file.txt"

would actually produce:

"c:my
ext

ile.txt"

(the double space is form feed, see here)




回答3:


Because when declaring a cstring literal the backslashes escape the next character, for special characters. This is so you can do newlines (\n), nulls (\0), carriage returns (\r) etc...

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



来源:https://stackoverflow.com/questions/46021357/could-someone-explain-c-escape-character-in-relation-to-windows-file-sys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!