Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include character literals?

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

like image 850
amanuel2 Avatar asked Oct 18 '16 16:10

amanuel2


People also ask

How do you write a character literal in Java?

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.

What is a char literal in C++?

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.

What are valid literal characters?

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 '~'.

How do you represent string literals?

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.


2 Answers

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;

}
like image 65
πάντα ῥεῖ Avatar answered Oct 27 '22 10:10

πάντα ῥεῖ


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
like image 22
Rakete1111 Avatar answered Oct 27 '22 10:10

Rakete1111