0

I am using Visual Studio 2022. I am trying to create a program that can read file like shaders. It would allow me not to have every single piece of code in the same file...

So, to clarify what I am trying to do:

  • I have written a shader in a separate file
  • I want to read this file
  • Then I want to build the shader from the file I just read

For that I tried to play around with "string", "stringstream"... I manage to get that :

std::string readShader(const char* path){
    std::string shader;
    std::ifstream file;
    file.open(path, std::ios::in);

    if (file.is_open()) {
    std::stringstream code;

    code << file.rdbuf();
    shader = code.str();

    file.close();
    }

    return shader;
}

I am not sure of the issue, but I was not able to build the shader.

In fact, I tried to print what I get by reading the file... Sometimes I get the shader and it is amazing but... I also get that kind of thing : ázý▓l☻

So, it surely is the issue.

I mean, what is that: ázý▓l☻?

Why do I get that?

That is not the content of my file!

I think it is because my function "return" something that end up deleted (or a part of it...).

Could you explain me what is the issue? how to solve it? and also why do I get that?

Smith14
  • 13
  • 4
  • Any reason not to use `ios::binary` in `open()`? – genpfault Apr 13 '23 at 20:23
  • 1
    Hello @genpfault, not really. I do not know what is the difference between ios::binary and ios::in for instance. So I automatically used ios::in. – Smith14 Apr 13 '23 at 20:39
  • @genpfault I do not know if you are the one who linked the question "What is a dangling pointer" but it helped me. Indeed, I was using an object that was deleted after the call of the function, and so non-valid anymore. Thank you a lot, it helped. – Smith14 Apr 13 '23 at 20:47

0 Answers0