Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

string - Could someone explain C++ escape character " " in relation to Windows file system?

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:myfoldermyfile.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:myfoldermyfile.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. 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:myfolderfile.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:myfolderfile.txt)"

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...