-1

When i create std::string and define it in program like this and print it to console it looks like this:

std::string shader = "#version 330\nlayout(location = 0) in vec3 vp;void main(){gl_Position = vec4(vp, 1.0);}";
printf("Original: %s\n", shader.c_str());

Output: First output when defined in program and printed out

But when i load std::string from file the way below and then print it to console it looks like this:

std::string shader;
std::ifstream inputStream(path, std::ifstream::in);
if (inputStream.is_open())
{
    while (inputStream.good())
    {
        std::string line;
        getline(inputStream, line);
        shader.append(line);
    }
}
printf("LOADED: %s\n", shader.c_str());

Text file:

#version 330\n
layout(location=0) in vec3 vp;
void main ()
{
     gl_Position = vec4 (vp, 1.0);
}

Output: Second output when loaded from file and printed out

I need it do be same when loading from file. To be more specific i need it to use the special character \n.

When i try to remove '\n': pic3

The problem here is that i need new line between "..330" and "layout..."

Wortig
  • 337
  • 2
  • 16

3 Answers3

2

\n and similar formations are called "escape sequences" and they are handled by the compiler when it translates your code.

So functions like printf never see them and don't know how to handle them. Instead, they see the real "new line" character, which is invisible, and has the character code 10 (0x0a).

When you are writing text in a text editor (like notepad), it will add this character automatically every time you press the Enter key, and it will also be saved to file. You can see these characters in any text file if you view it with a hex editor.

(Note, on Windows, 2 characters might be added "new line" and "carriage return" which is 13 or 0x0d)

So, you have two options:

  1. Don't use \n in your input file, instead just break up the lines as usual, and use istream::read or similar function to read all bytes in the file.

  2. If you are not allowed to change the input file, write a function that searches the string for "\n" (sub string of 2 characters) and replaces them with a single literal \n (0x0a)

There are plenty of examples on the web, here is one: Convert string with explicit escape sequence into relative character

Unfortunately, there is no preexisting function to do this in the standard library, but the code to write your own is short and simple, and it will be even more simple if you only need to translate \n and no other escaped characters.

Lev M.
  • 4,162
  • 1
  • 6
  • 21
  • Re: 'write a function that searches the string for "\n" ' -- the code has to search for `"\\n"`. That's two characters, a backslash followed by the letter n. `"\n"` is one character. +1. – Pete Becker Oct 03 '20 at 12:31
0

getline just reads the input line by line, stopping to the first carriage return it will find. Adding manually a \ and a n in the file will only make getline interpret it as more text, it just waits for a real carriagr return. All you have to do is add the \n in your .append() call.

  • But is not there a way without calling .append with \n? – Wortig Oct 03 '20 at 08:59
  • Because i do not know how many new lines i will need when the file i am loading gets larger. – Wortig Oct 03 '20 at 09:08
  • 1
    @Wortig It seems you want to read and output the whole file. So just read the file one character at a time and append all the characters to your string. There's no need to treat newlines as special in any way. – john Oct 03 '20 at 09:08
  • @john Thank you so much, it seems it solved my problem when reading by each character. – Wortig Oct 03 '20 at 09:19
  • @Wortig where was absolutely no need, using getline works, why couldn't you add a "\n" ? – Sylvain Chaugny Oct 03 '20 at 22:02
0

Solution was to read text file by each character and then printing the string is correct.

std::string shader;
std::ifstream inputStream(path, std::ifstream::in);
char character;
if (inputStream.is_open())
{
    while (inputStream.get(character))
    {
        shader += character;
    }
}

Output when printing shader:

enter image description here

Adding \n after each getline should work too.

Wortig
  • 337
  • 2
  • 16