C++ 错误不完整的通用字符名称\U

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4084283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:31:01  来源:igfitidea点击:

Error incomplete universal character name \U

c++text-files

提问by rectangletangle

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

我正在尝试编写一个更改 .txt 文件的 C++ 程序。但是,当我运行它时,我收到一个奇怪的错误。

The error:

错误:

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

6:20 C:\Dev-Cpp\Homework6.cpp 不完整的通用字符名\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();
  }
  else cout << "Unable to open file";
  return 0;
}

What am I doing wrong?

我究竟做错了什么?

回答by Billy ONeal

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

"C:\Users\My Name\Desktop\test\input.txt"
反斜杠 ( \) 是一个特殊字符。你必须逃避它:
"C:\\Users\\My Name\\Desktop\\test\\input.txt"

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

编辑:或者,使用正斜杠 ( /)。Windows 不在乎。

回答by Adam Rosenfield

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

您需要在文件名中转义反斜杠。在 C++ 字符串常量中,反斜杠是一个不代表自身的转义字符。要获得文字反斜杠,您需要使用双反斜杠\\

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

\U是 32 位 Unicode 转义序列的前缀:您可以使用诸如“ \U0010FFFF”之类的东西来表示高位 Unicode 字符。编译器抱怨这\Users...不是有效的 Unicode 转义序列,因为sers...它不是有效的十六进制数。

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

解决方法是使用 string "C:\\Users\\My Name\\Desktop\\test\\input.txt"

回答by Ben Hymanson

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

您需要在那里使用双反斜杠。所以"C:\\Users...。否则,您将启动一个转义序列(在本例中为 \U 表示 unicode 文字)。

回答by Naveen

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

您需要在文件名中\使用额外的字符来转义\。(即你需要使用\\

回答by Adem

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

这是确切的情况,但\U与 的含义不同\u。iOS 接受\u并抱怨\U

回答by guest

This error happens even in Visual Studio 2015 even if the text is in commentsof 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.

即使在 Visual Studio 2015 中,即使文本位于C/C++ 源代码的注释中,也会发生此错误。Visual Studio 不够聪明,无法忽略注释中的此文本,即使该命令告诉了一些有用的信息(例如,在单词 domain\user 中,并且如果此文本在例如配置文件中按字面意义出现)。奇怪的。