我正在尝试从 zip 文件内加载着色器源代码,该文件是用记事本创建的纯文本文件。加载代码如下(从下面的代码片段中删除了错误检查代码):
std::string retrievestringfromarchive(std::string filename)
{
//data is the zip resource attached elsewhere
unz_file_info info;
Uint8* rwh;
unzLocateFile(data, filename.c_str(), NULL);
unzOpenCurrentFile(data);
unzGetCurrentFileInfo(data, &info, NULL, 0, NULL, 0, NULL, 0)
rwh = (Uint8*)malloc(info.uncompressed_size);
unzReadCurrentFile( data, rwh, info.uncompressed_size );
//garbage at end of file
const char* rwh1 = reinterpret_cast<char*>(rwh);
std::stringstream tempstream(rwh1);
std::string tempstring = tempstream.str();
free(rwh);
return tempstring;
}
返回的字符串输出如下:
//FRAGMENT SHADER
#version 120
//in from vertex shader
varying vec2 f_texcoord;
varying vec4 f_color;
uniform sampler2D mytexture;
void main(void)
{
gl_FragColor = texture2D(mytexture, f_texcoord) * f_color;
}
//endfile««««««««îþîþ
注释:
我检查了信息结构,压缩和未压缩的大小都与 7zip 中的信息匹配
当用 gdb 检查时,缓冲区“rwh”本身在末尾有垃圾字符
我是Win7 64位,使用codeblocks和TDM-GCC-32 4.8.1编译
“//endfile”注释巧妙地避免了 gl 着色器编译问题,但这已经过去了。
最佳答案
rwh = (Uint8*)malloc(info.uncompressed_size); unzReadCurrentFile( data, rwh, info.uncompressed_size );
我非常怀疑 unzReadCurrentFile
在缓冲区中添加了 0
终止符 - 无论如何都不会有空间 - 并且您将指针用作 0
-终止字符串。
如果将缓冲区解释为字符串确实有意义,您可以这样做:
std::string tempstring(rwh1, info.uncompressed_size);
关于c++ - 使用 minizip/zlib 从 zip 存档加载文本文件,文件末尾有垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23482600/