问题
I'm trying to use back slash in C++ in a string like this :
HWND hwnd = FindWindowA(NULL, "C:\Example\App.exe");
So for this example I would get these errors/warnings :"unknown escape sequence: '\E'" "unknown escape sequence: '\A'" . Since I need to type in the exact name of the window , is there any way to avoid using back slashes or stop the compiler from interpreting them as "escape sequences" ?
回答1:
You have to escape them properly, C++11 added raw string which eases this thing:
HWND hwnd = FindWindowA(NULL, R"(C:\Example\App.exe)");
else do it manually:
HWND hwnd = FindWindowA(NULL, "C:\\Example\\App.exe");
回答2:
You should escape that properly:
HWND hwnd = FindWindowA(NULL, "C:\\Example\\App.exe");
For a full list of all escape sequences, check this:
https://en.cppreference.com/w/cpp/language/escape
回答3:
you can escape backslash doubling them:
HWND hwnd = FindWindowA(NULL, "C:\\Example\\App.exe");
回答4:
Inside a string literal a backslash is the first character of a character escape sequence. So "\n"
consists of two characters: a newline character (from the \n
) and a null character (because it's a string literal).
So, to get a backslash into the text, you need an escape character that represents a backslash. Simple enough: "\\"
consists of two characters: a backslash character (from the \\
) and a null character (because it's a string literal).
Another possibility is using a "raw string literal", which ignores escape sequences. R"(\n\\)"
consists of five characters: a backslash character, an n
character, two more backslash characters, and a null character (because it's a string literal).
来源:https://stackoverflow.com/questions/53956020/back-slash-causing-problems-c