Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error incomplete universal character name \U

Tags:

c++

text-files

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();
  }
  else cout << "Unable to open file";
  return 0;
}

What am I doing wrong?

like image 295
rectangletangle Avatar asked Nov 03 '10 04:11

rectangletangle


3 Answers

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

like image 141
Billy ONeal Avatar answered Oct 15 '22 07:10

Billy ONeal


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

like image 22
Adam Rosenfield Avatar answered Oct 15 '22 07:10

Adam Rosenfield


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

like image 29
Ben Jackson Avatar answered Oct 15 '22 06:10

Ben Jackson