简体   繁体   English

从文件读取到字符串无法正常工作

[英]Reading from a file to a string doesn't work as expected

I'm currently writing my first programm in OpenGl and C/C++. 我目前正在用OpenGl和C / C ++编写我的第一个程序。 I came across the following problem: 我遇到以下问题:

I wrote a method which reads a file (in my case containing the vertex or fragment shader) and returns the content as one string. 我编写了一种方法,该方法读取一个文件(在我的情况下包含顶点或片段着色器),然后将内容作为一个字符串返回。

std::string loadShader(const char* filepath)
{
    if(filepath){

        std::ifstream ifs(filepath, std::ifstream::in);
        std::ostringstream oss;
        std::string temp;

        while(ifs.good()){

            getline(ifs, temp);
            oss << temp << '\n';
            std::cout << temp << std::endl;

        }
        ifs.close();
        return oss.str();

    }
    else{
       exit(EXIT_FAILURE);
    }
}

I've two files: vertexShader.glsl and fragmentShader.glsl which I want to convert to two seperate strings for the creation of the shaders. 我有两个文件:vertexShader.glsl和fragmentShader.glsl,我想将其转换为两个单独的字符串以创建着色器。 The code above is called in the following way: 上面的代码按以下方式调用:

void createShaders(void){

    GLenum errorCheckValue = glGetError();

    const GLchar* vertexShader = loadShader("vertexShader.glsl").c_str();
    const GLchar* fragmentShader = loadShader("fragmentShader.glsl").c_str();

    vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShaderId, 1, &vertexShader, NULL);
    glCompileShader(vertexShaderId);

    fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShaderId, 1, &fragmentShader, NULL);
    glCompileShader(fragmentShaderId);

    // some other code follows...
}

I debugged the code above and noticed that the vertexShader const char* is empty, while the fragmentShader const char* contains the correct code (the content from the file I just converted to a string). 我调试了上面的代码,发现vertexShader const char*为空,而fragmentShader const char*包含正确的代码(我刚刚转换为字符串的文件中的内容)。

I don't understand the inconsistency in the method. 我不了解该方法的不一致之处。 I can't figure out, how one shader is parsed the right way and the other is empty. 我不知道如何正确地解析一个着色器,而另一个则是空的。

I hope somebody can figure it out. 我希望有人能弄清楚。 Thank you very much. 非常感谢你。

The problem is that your unnamed std::string returned from loadShader() only exists within the scope of the expression: 问题是,从loadShader()返回的未命名std::string仅存在于表达式的范围内:

const GLchar* vertexShader = loadShader("vertexShader.glsl").c_str();

Quoting the standard: 引用标准:

Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. 临时对象被销毁,是评估(按词法)包含创建它们的点的完整表达式(1.9)的最后一步。

The fix is simple: 解决方法很简单:

const std::string vertexShaderStr = loadShader("vertexShader.glsl");
const GLchar *vertexShader = vertexShaderStr.c_str();
const std::string fragmentShaderStr = loadShader("fragmentShader.glsl");
const GLchar *fragmentShader = fragmentShaderStr.c_str();

Change this: 更改此:

const GLchar* vertexShader = loadShader("vertexShader.glsl").c_str();
const GLchar* fragmentShader = loadShader("fragmentShader.glsl").c_str();

To this: 对此:

std::string vertexShaderString = loadShader("vertexShader.glsl");
std::string fragmentShaderString = loadShader("fragmentShader.glsl");
const GLchar* vertexShader = vertexShaderString.c_str();
const GLchar* fragmentShader = fragmentShaderString.c_str();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM