8

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

zondo
  • 18,070
  • 7
  • 35
  • 73
amanuel2
  • 4,268
  • 2
  • 29
  • 61
  • 1
    Do you need to read the file at runtime? Otherwise you could probably make use of the preprocessor. – πάντα ῥεῖ Oct 18 '16 at 16:52
  • No i dont need to , @πάνταῥεῖ . What do you mean i can make use of a preprocessor? .. `#define toStr(x) #x` ? – amanuel2 Oct 18 '16 at 16:53
  • Here is a link which does what you want: [link](http://stackoverflow.com/questions/5612182/convert-string-with-explicit-escape-sequence-into-relative-character) – Rikocar Oct 18 '16 at 16:54
  • @amanuel2 You may adjust your `Program.b` a bit, and use a `#include "Program.b"` statement to initialize a `std::string` variable for example. – πάντα ῥεῖ Oct 18 '16 at 16:57
  • @Rikocar .. hmm the `unescape(file_contents);` , doesn't work using the `unescape` function from that particular answer that got accepted. – amanuel2 Oct 18 '16 at 16:57
  • @πάνταῥεῖ Program.b is not a C++ File , just saying , just a BoneLang File :P . and what do you mean exactly ? – amanuel2 Oct 18 '16 at 16:59
  • @Rikocar oh seems to work when i just print out the return statement `cout << unescape(file_contents) << endl;` – amanuel2 Oct 18 '16 at 17:03

2 Answers2

6

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;

}
πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
  • Wow, that is hacky though :) – Rakete1111 Oct 18 '16 at 17:03
  • @Rakete1111 hmm limits BoneLang Capability. because i have to use R . I got another way though that doesn't require `R`. Thanks this will be helpfull to readers – amanuel2 Oct 18 '16 at 17:05
  • @amanuel2 _"because i have to use R_ Well, unfortunately you can't put that outside of the `Program.b` file, since `#include` doesn't work within double quotes. I was asking something similar here, give me a minute. – πάντα ῥεῖ Oct 18 '16 at 17:09
  • @amanuel2 Here it is: http://stackoverflow.com/questions/37622767/is-there-a-way-to-pull-in-a-text-resource-into-a-raw-string-literal-using-the-pr – πάντα ῥεῖ Oct 18 '16 at 17:10
  • That seems a "hackish" way :P . See [**this**](https://gist.github.com/amanuel2/b8d02b026715e54857313066c799316d) gist . gotta go next period chaw!\ – amanuel2 Oct 18 '16 at 17:15
  • @amanuel2 Extended my answer, based on looking to your gist. Seems the more appropriate way for that case. – πάντα ῥεῖ Oct 18 '16 at 17:31
2

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
Rakete1111
  • 42,521
  • 11
  • 108
  • 141
  • Wow seems like a good idea but that will be kinda tedious when you need more. Should i post my solution to? – amanuel2 Oct 18 '16 at 17:07
  • @amanuel2 You can always make a generic function that will remove any special characters with their actual representation. – Rakete1111 Oct 18 '16 at 17:08
  • Thanks. See [**this**](https://gist.github.com/amanuel2/b8d02b026715e54857313066c799316d) gist . gotta go next period chaw! – amanuel2 Oct 18 '16 at 17:15