I have a question on how to include string literals when getting info from a file. Let me show my code for better understanding:
Program.b:
print \"Hello World\n\"; print \"Commo Estas :)\n\"; print \"Bonjour\";print \"Something\"; return 0;
main.cpp (I have minimized the actual file to what is needed for this question):
int main()
{
std::string file_contents;
std::fstream file;
file.open("Program.b");
std::ifstream file_read;
file_read.open("Program.b");
if(file_read.is_open())
while(getline(file_read,file_contents));
cout << file_contents << endl;
}
So right now when I print file_contents
, I get:
print \"Hello World\n\"; print \"Commo Estas :)\n\"; print \"Bonjour\";print \"Something\"; return 0;
You can see it includes the \n
. Is there a way to make that an actual character literal, so printing it actually prints a new line? (I would want the same for quotation marks.)
Java Character literals A character literal represents a single character that is enclosed in a single quote '', as in 'Z'. The rule for writing a character literal is that it must contain a single character enclosed within a single quote. We have to use the char data type to represent a character literal.
A character literal is a type of literal in programming for the representation of a single character's value within the source code of a computer program. In C++, A character literal is composed of a constant character. It is represented by the character surrounded by single quotation marks.
Character literals are enclosed in single quotation marks. Any printable character, other than a backslash (\), can be specified as the single character itself enclosed in single quotes. Some examples of these literals are 'a', 'A', '9', '+', '_', and '~'.
A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.
Try something like this:
Program.b
R"inp(print "Hello World\n"; print "Commo Estas :)\n"; print "Bonjour";print "Something"; return 0;)inp"
main.cpp
int main() {
std::string file contents =
#include "Program.b"
;
std::cout << file_contents << std::endl;
}
You can also change Program.b
to make it a bit more readable:
R"inp(
print "Hello World\n";
print "Commo Estas :)\n";
print "Bonjour";
print "Something";
return 0;
)inp"
The runtime variant should be simply:
Program.b
print "Hello World\n";
print "Commo Estas :)\n";
print "Bonjour";
print "Something";
return 0;
main.cpp
int main()
{
std::string file_contents;
std::fstream file;
file.open("Program.b");
std::ifstream file_read;
file_read.open("Program.b");
if(file_read.is_open()) {
std::string line;
while(getline(file_read,line)) {
file_contents += line + `\n`;
}
}
cout << file_contents << endl;
}
You can do a simple find + replace.
std::size_t pos = std::string::npos;
while ((pos = file_contents.find("\\n")) != std::string::npos)
file_contents.replace(pos, 1, "\n");
//Every \n will have been replaced by actual newline character
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With