Incomplete universal character name error \ U

I am trying to write a C ++ program that modifies a .txt file. However, when I start, I get a strange error.

Error:

6:20 C: \ Dev-Cpp \ Homework6.cpp incomplete universal symbol name \ U

My code is:

#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile ("C:\Users\My Name\Desktop\test\input.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile << "This is another line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0; } 

What am I doing wrong?

+4
source share
6 answers

"C:\Users\My Name\Desktop\test\input.txt"
The backslash ( \ ) is a special character. You should avoid this:
"C:\\Users\\My Name\\Desktop\\test\\input.txt" .

EDIT: Alternatively, use slashes ( / ). Windows does not care.

+18
source

You need to avoid backslashes in the file name. In C ++ string constants, the backslash is an escape character that does not represent itself. To get a literal backslash, you need to use a double backslash \\ .

\U is a prefix for the Unicode 32-bit escape sequence: you should use something like " \U0010FFFF " to represent a high Unicode character. The compiler complains that \Users... not a valid Unicode escape sequence, since sers... not a valid hexadecimal number.

The fix is ​​to use the string "C:\\Users\\My Name\\Desktop\\test\\input.txt" .

+6
source

You need to use double backslash. So "C:\\Users... Otherwise, you run the escape sequence (in this case, \ U for the unicode literal).

+3
source

You need to escape \ with an extra \ in the file name. (i.e. you need to use \\ )

+2
source

this is the exact case, but \U does not matter with \U iOS accepts \U and complains about \U

+2
source

This error occurs even in Visual Studio 2015, even if the text is in the comments of your C / C ++ source. Visual Studio is not smart enough to ignore this text in the comments, even if the command reports something useful (for example, in the user \ user domain and if this text is literally expected in the configuration file). Weird

0
source

All Articles