I am trying to load GLSL vertex/fragment shader sources into const char* array to be used with OpenGL. The function I am using to do that is
const char* loadSource(const char* path)
{
string line;
stringstream output;
ifstream source(path);
if(source.is_open())
{
while(source.good())
{
getline(source, line);
output << line << endl;
}
source.close();
}
return output.str().c_str();
}
The source input works fine on first call. However if I call for the second source, the first source gets "corrupted" (both calls are inside scope of a single function):
const char* vs_source = loadSource("vertex.vert"); // vs_source loads fine
const char* fs_source = loadSource("fragment.frag"); // fs_source loads fine. vs_source is terminated sooner than after previous call.
Note: I tried to code vs_source directly into *.cpp file and both shaders compiled. This points out to the fact that I must be doing something silly in the loadSource function.
Question: What causes such weird behaviour of the text input?