C++ raw string with special char [duplicate] - c++

This question already has answers here:
escape R"()" in a raw string in C++
(2 answers)
Include )" in raw string literal without terminating said literal
(3 answers)
Closed 9 months ago.
I want to output a string like this: onclick="func()". So I wrote the following code:
std::string s = R"(
onclick="func()"
)";
But here two )" let the compiler confused.
Please forgive me if it's a silly question.
I googled but found nothing (I don't know which keyword I should use when I googled).

Simply add a unique string outside the ()
std::string s = R"anystring(
onclick="func()"
)anystring";

Related

sprintf cause programm stopping [duplicate]

This question already has answers here:
printf with std::string?
(9 answers)
Closed 7 months ago.
std::string sszModName = "kernel32.dll";
std::string WinVersion = "WIN81";
std::string MachineGUID= "ce9e95db-5fda-436a-b29a-f5537702c77d";
char buf[1024];
sprintf(buf, "https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=%s&publisher=%s&win=%s&machineguid=%s", sszModName, "ERROR_HASH_VERIFY", WinVersion, MachineGUID);
This code causes program lag, could you help me figure out why?
Try
sprintf(buf,
"https://nulln.nullnu-ll.nul/nullnulln/api/ireport.php?module=%s&publisher=%s&win=%s&machineguid=%s",
sszModName.c_str(),
"ERROR_HASH_VERIFY",
WinVersion.c_str(),
MachineGUID.c_str());
C strings are not the same as C++ strings. spprintf only uses C strings so you must use .c_str() to turn your C++ strings into C strings.

Replace preprocessor macro of string literal with concatenation [duplicate]

This question already has answers here:
Simplest way to combine two strings at compile time with C++11
(2 answers)
How to concatenate static strings at compile time?
(2 answers)
Concatenate compile-time strings in a template at compile time?
(4 answers)
Closed 8 months ago.
I am attempting to replace as many uses of macros in our code with proper c++17 constructs. How would I replace the following macro with a constexpr or something else?
#define FNAME "first"
#define LNAME "last"
#define NAME FNAME LNAME
const char hello[] = "hello " NAME;

How can I use std::string for Const Char Parameter [duplicate]

This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 2 years ago.
I am trying to use the std::rename() function to move a .docx file, however, the name of the file may vary. How can I use a std::string within std::rename() so that it does not have to be a hardcoded filepath, like this std::rename(filepath, destination);?
I don't know how you want to populate the strings in question, but here you go:
std::string fromName {"whatever you're going to do"};
std::string toName {"whatever you're going to do"};
std::rename(fromName.c_str(), toName.c_str());

(C++) How to convert a string into a character vector? [duplicate]

This question already has answers here:
How to copy std::string into std::vector<char>? [duplicate]
(2 answers)
Closed 7 years ago.
How to convert a string such as "Hello, world!" into a vector of characters? I've seen many techniques on how to do this for plain arrays, but none for vectors. Here is my code:
string raw_text = "Hello, world!";
vector<char> char_text;
Any way to make a character vector from raw_text?
Thanks in advance!
vector<char> char_text(raw_text.begin(), raw_text.end());

Some questions with printing output in C++ [duplicate]

This question already has answers here:
What is the type of a string literal in C++? [duplicate]
(2 answers)
Closed 7 years ago.
When I use :
std::cout << "Hello world ";
Which type is "Hello world" ?
Where does it stored , so I can get it out and work with it ?
For some reasons, I don't want to use something like :
std::string str = "Hello world";
std::cout << str;
Please help me, I searched an hour but still no answer.
The type of a string literal is "constant array of char", with as many elements as characters in the literal, plus one for a final null character. Other versions of string literals (wide, unicode) are arrays of other character types (wchar_t, char16_t etc.) (e.g. see here).