我正在使用OpenGL进行一些实验,我正在尝试加载着色器。它需要const char *中的源代码,但由于我在C ++上执行它,我可以使用std :: strings然后调用str.c_str()。这不是问题 - 当我尝试读取文件时,它读取完美,但返回的值是一个损坏的字符串。这是代码的相关部分:
// inline method on engine.hpp
inline void readFile(std::string filePath, std::string* retValue)
{
std::ifstream file;
file.open(filePath);
std::string result;
std::string line;
while (!file.eof())
{
std::getline(file, line);
result.append(line + "\n");
}
memcpy(retValue, &result, sizeof(result));
}
// implemented method on engine.cpp
GLint Engine::createShader(std::string vs, std::string fs)
{
GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);
std::string vsSourceStr = "";
std::string fsSourceStr = "";
readFile(vs, &vsSourceStr);
readFile(fs, &fsSourceStr);
const char* vsSource = vsSourceStr.c_str();
const char* fsSource = fsSourceStr.c_str();
//std::string t_vs = readFile(vs);
//const char* vsSource = readFile(vs).c_str();
//const char* fsSource = readFile(fs).c_str();
glShaderSource(vertex, 1, &vsSource, NULL);
glCompileShader(vertex);
glShaderSource(fragment, 1, &fsSource, NULL);
glCompileShader(fragment);
GLint program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
if (shaderCompiled(program))
{
std::cout << "shader successfully compiled" << std::endl;
}
else
{
std::cout << "shader not compiled" << std::endl;
printShaderError(vertex);
printShaderError(fragment);
std::cout << "Vertex Shader source:" << std::endl;
std::cout << vsSource << std::endl;
std::cout << "Fragment Shader source:" << std::endl;
std::cout << fsSource << std::endl;
}
return program;
}
以下是Visual Studio在调试时所说的内容:http://prntscr.com/4qlnx7
它完美地读取文件,只是崩溃了返回值。我已尝试使用引用和复制内存返回结果,正如您在我的代码中看到的那样。 不管怎样,谢谢。
答案 0 :(得分:3)
这不符合您的想法:
std::string line;
while (!file.eof())
{
std::getline(file, line);
result.append(line + "\n");
}
请使用:
std::string line;
while (std::getline(file, line))
{
result.append(line + "\n");
}
原因是在读取文件之后才会触发eof()
。这意味着您的std::getline()
可能已失败(在EOF处)并且您正在使用该错误数据。
请参阅: C++ FAQ 15.5关于eof()
。
答案 1 :(得分:0)
执行memcpy(retValue, &result, sizeof(result));
时,您正在复制内部std :: string结构,而不是字符串数据。改为分配字符串:*retValue = result
将结果字符串的引用用于readFile()将获得更强大的版本:
void readFile(std::string filePath, std::string& retValue)
{
std::ifstream file(filePath);
retValue.clear();
std::string line;
while (std::getline(file, line))
{
retValue += line;
retValue += '\n';
}
}
GLint Engine::createShader(std::string vs, std::string fs)
{
GLuint vertex = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment = glCreateShader(GL_FRAGMENT_SHADER);
std::string vsSourceStr;
std::string fsSourceStr;
readFile(vs, vsSourceStr);
readFile(fs, fsSourceStr);
// ...
}
将文件读入字符串的其他方法是使用std::istreambuf_iterator
:
inline std::string readFile(std::string const& filename)
{
std::ifstream file(filename);
std::istreambuf_iterator<char> begin(file), end;
return std::string(begin, end);
}