O'con
O'con

Reputation: 184

How to define a multiline wstring with content from another file

I would like to define the content of a file in a wstring, so I can print it with an ofstream later on.

Example:

// Working
std::wstring file_content=L"€";
// Working
file_content=L"€"
L"€"
L"€";
// NOT Working
file_content=L"€"
L"€""
L"€";
// Working
file_content=LR"SOMETHING(multiline
with
no
issues)SOMETHING";

For some reason, the last solution is not working for me WHEN I paste in the file content (Multibyte).

Errors:

E0065 expected a ';'

E2452 ending delimiter for raw string not found

image

Upvotes: 0

Views: 126

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595897

You need to escape double-quote characters inside of a string literal, eg:

// Working
file_content=L"€"
L"€\"" // <--
L"€";

Alternatively, use a raw string literal instead, which does not require its characters to be escaped, eg:

// Working
file_content=L"€"
LR"(€")" // <--
L"€";

Upvotes: 1

std::wstring file_content=LR"SOMETHING(
€
€"
€
)SOMETHING";

Here, SOMETHING is something not found in the string and not required but it enables you to use " inside the multiline string literal without having to escape it.

Upvotes: 0

Related Questions