开发者

Error incomplete universal character name \U

开发者 https://www.devze.com 2023-01-23 19:56 出处:网络
I\'m trying to write a C++ program that alters a .txt file. However when I run it I get a strange error.

I'm trying to write a C++ program that alters a .txt file. However when I run it I get a strange error.

The error:

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

My code:

#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()开发者_JAVA百科;
  }
  else cout << "Unable to open file";
  return 0;
}

What am I doing wrong?


"C:\Users\My Name\Desktop\test\input.txt"
The backslash (\) is a special character. You must escape it:
"C:\\Users\\My Name\\Desktop\\test\\input.txt".

EDIT: Alternately, use forward slashes (/). Windows doesn't care.


You need to escape your backslashes in the filename. In C++ string constants, backslash is an escape character which doesn't represent itself. To get a literal backslash, you need to use a double backslash \\.

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

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


You need to use double backslashes there. So "C:\\Users.... Otherwise you're starting an escape sequence (in this case \U for a unicode literal).


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


it is exact case but, \U is not same meaning with \u. iOS accepts \u and it complains to \U


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

0

精彩评论

暂无评论...
验证码 换一张
取 消