用minizip/zlib从zip存档中加载文本文件,文件末尾有垃圾字符
Loading text file from zip archive with minizip/zlib, garbage characters at end of file
我试图从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
中的信息匹配。缓冲区"rwh"本身在末尾有垃圾字符,当使用gdb
检查时我是在Win7 64位,使用代码块和TDM-GCC-32 4.8.1编译
"//endfile"注释巧妙地避免了gl shader编译问题,但这是必须的
rwh = (Uint8*)malloc(info.uncompressed_size); unzReadCurrentFile( data, rwh, info.uncompressed_size );
我非常怀疑unzReadCurrentFile
在缓冲区中添加了0
终止符-无论如何都没有空间-并且您正在使用指针作为0
终止的字符串。
如果将缓冲区解释为字符串确实有意义,您可以这样做:
std::string tempstring(rwh1, info.uncompressed_size);
解压缩器给你一个解压缩的数据块——但它不知道数据是纯文本,也不知道你打算把它用作c语言字符串。因此,它不会在末尾附加一个终止NUL(零)字符。这一切。只需复制给定数量的字符,并且不假设数据块以零结尾。
相关文章:
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何从文本文件中读取数值,直到遇到字符类型?
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 尝试将字符串从文件读取到无符号字符向量中
- 如何在C++中反转文本文件中字符的顺序?
- 生成随机.txt文件,10行,10列。充满随机:"*"、"#"、"@"。然后将随机字符替换为'P'
- 如何存储 unicode 字符并将其输出到文件?
- 无法将整个文本文件复制到字符数组
- C++逐个字符读取二进制文件字符
- 如何将所有文件字符转换为大写或小写
- 如何通过C 读取CHAR的文件字符
- 无法写入文件字符指针
- 在 c++ 中逐个从文件字符读取
- Boost:如何检查文本文件字符编码/字符集
- 文件字符编码
- c++ -读取文件字符时的无限循环
- 创建一个大尺寸的char类型数组并复制文件字符