reading a file with std::ifstream to compile a OpenGL shader doesn't work
<p>Here is my little function I'm having trouble with:</p>

<pre><code>GLuint LoadShaders()
{
    // open and compile vertex shader
    std::ifstream VSS("glfwtest.vert", std::ifstream::ate);
    int len = VSS.tellg() + 1;
    GLchar* vertexSource = new GLchar[len];
    VSS.clear();
    VSS.seekg(0, VSS.beg);
    for(int i=0; i&lt;len ; i++) { VSS.get(vertexSource[i]); }

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &amp;vertexSource, NULL);
    glCompileShader(vertexShader);
    VSS.close();

    // open and compile the fragment shader
    std::ifstream FSS("glfwtest.frag", std::ifstream::ate);
    len = FSS.tellg() + 1;
    GLchar* fragmentSource = new GLchar[len];
    FSS.clear();
    FSS.seekg(0, FSS.beg);
    for(int i=0; i&lt;len ; i++) { FSS.get(fragmentSource[i]); }

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &amp;fragmentSource, NULL);
    glCompileShader(fragmentShader);
    FSS.close();

    // Link the vertex and fragment shader into a shader program
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glBindFragDataLocation(shaderProgram, 0, "outColor");
    glLinkProgram(shaderProgram);
    return shaderProgram;
}
</code></pre>

<p>I have followed <a href="https://open.gl/drawing" rel="nofollow noreferrer">this</a> guide and I managed to render the white triangle correctly. After that I thought it might be a good exercise to split up the whole thing into functions to reuse them later. So I put my shader source code for both shader in seperate files(instead having them directly in my C++ source), and wrote the function above to read them from their respective files. </p>

<p>Now here's the catch: my vertex shader loads and compiles fine, while my fragment shader does not. But when I put my fragment shader source into the code like this</p>

<pre><code>const GLchar* fragmentSource =
    "#version 150 core\n"
    "out vec4 outColor;"
    "void main()"
    "{"
    "    outColor = vec4(1.0, 1.0, 1.0, 1.0);"
    "}";

GLuint LoadShaders()
{

    [...]

    // open and compile the fragment shader
    //std::ifstream FSS("glfwtest.frag", std::ifstream::ate);
    //len = FSS.tellg() + 1;
    //GLchar* fragmentSource = new GLchar[len];
    //FSS.clear();
    //FSS.seekg(0, FSS.beg);
    //for(int i=0; i&lt;len ; i++) { FSS.get(fragmentSource[i]); }

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &amp;fragmentSource, NULL);
    glCompileShader(fragmentShader);

    [...]

    return shaderProgram;
}
</code></pre>

<p>it works fine and shows a white triangle. So the same code that loads and compiles my vertex shader from "glfwtest.vert" does not work for the fragment shader. </p>

<p>I have never worked with <code>&lt;fstream&gt;</code> in C++ before and put it together from the <a href="http://www.cplusplus.com/reference/istream/istream/seekg/" rel="nofollow noreferrer">online documentation example</a> I found. I guess the error is somewhere there but I can't find it since I am not a C++ wizard (yet).</p>

<p><strong>EDIT:</strong></p>

<p>After the suggestions in the comments I'm still stuck. Here's the gdb output I get:</p>

<pre><code>(gdb) break main.cpp:55
Breakpoint 1 at 0x4017dc: file main.cpp, line 55.
(gdb) r
Starting program: /home/georg/code/redbook/glfwtest/glfwtest 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".

Breakpoint 1, LoadShaders () at main.cpp:56
56      GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
(gdb) p vertexSource
$1 = (GLchar *) 0x8277e0 "#version 420 core\n\nin vec2 position;\n\nvoid main()\n{\n    gl_Position = vec4(position, 0.0, 1.0);\n}"
(gdb) p fragmentSource
$2 = (GLchar *) 0x82a300 "#version 150 core\nout vec4 outColor;\nvoid main()\n{\n   outColor = vec4(1.0, 1.0, 1.0, 1.0);\n}\377\177"
(gdb) p len
$3 = 92
(gdb) 
</code></pre>

<p>As you can see, the length is correctly stored in <code>len</code>, there are 92 chars in the source. Still, somehow there are two extra chars. I checked the source file even with a hex editor, those two chars are definitely not in the file.</p>