Reading from a file in C++ outputs weird characters

beginner here.

I just wrote some simple code to read a file and it is (sort of) working:
BUT: No matter what I do, I get “²” at the end of my output.

Here´s my file reading code:

std::string MSEshaderprogram::readFile(const std::string& filepath)

        std::ifstream file{ filepath, std::ios::ate | std::ios::binary };
        if (!file.is_open())

            MSEdebug::getInstance().debugError("Failed to open: " + filepath);


        size_t fileSize = static_cast<size_t>(file.tellg());
        std::vector<char> buffer(fileSize);

        file.seekg(0);, fileSize);

        std::string output =;

        return output;


And here´s the output I get when I look at my debug Output: (yes I´m trying to read a vertex shader)

#version 330 core

layout (location = 0) in vec3 aPos;

void main()
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);

Already thank you in advance for answering.


std::string output =;

Because data is not null-terminated, your output string is formed incorrectly. Use the constructor which also takes the length, i.e.

std::string output(, buffer.size());

Alternatively, make sure buffer is null-terminated (by taking it one byte larger and setting the last byte to 0)