c++ Zlib写入文件不工作
c++ Zlib fwrite to file not working
我有这个程序,我正在工作,简单地做以下操作:
- 打开压缩文件
- 解压成std::string
- 从std::string压缩
- 另存为gzip文件
然而,我似乎不能在最后写入压缩的数据。我得到的错误是:没有匹配的函数调用'fwrite'。
我是否为它提供了错误的参数?或者输入错误的类型?
完整的过程如下:
#include <iostream>
#include "zlib-1.2.8/zlib.h"
#include <stdexcept>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <assert.h>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include "zlib.h"
#include "zconf.h"
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif
#define CHUNK 16384
using namespace std;
bool gzipInflate( const std::string& compressedBytes, std::string& uncompressedBytes ) {
if ( compressedBytes.size() == 0 ) {
uncompressedBytes = compressedBytes ;
return true ;
}
uncompressedBytes.clear() ;
unsigned full_length = compressedBytes.size() ;
unsigned half_length = compressedBytes.size() / 2;
unsigned uncompLength = full_length ;
char* uncomp = (char*) calloc( sizeof(char), uncompLength );
z_stream strm;
strm.next_in = (Bytef *) compressedBytes.c_str();
strm.avail_in = compressedBytes.size() ;
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
bool done = false ;
if (inflateInit2(&strm, (16+MAX_WBITS)) != Z_OK) {
free( uncomp );
return false;
}
while (!done) {
// If our output buffer is too small
if (strm.total_out >= uncompLength ) {
// Increase size of output buffer
char* uncomp2 = (char*) calloc( sizeof(char), uncompLength + half_length );
memcpy( uncomp2, uncomp, uncompLength );
uncompLength += half_length ;
free( uncomp );
uncomp = uncomp2 ;
}
strm.next_out = (Bytef *) (uncomp + strm.total_out);
strm.avail_out = uncompLength - strm.total_out;
// Inflate another chunk.
int err = inflate (&strm, Z_SYNC_FLUSH);
if (err == Z_STREAM_END) done = true;
else if (err != Z_OK) {
break;
}
}
if (inflateEnd (&strm) != Z_OK) {
free( uncomp );
return false;
}
for ( size_t i=0; i<strm.total_out; ++i ) {
uncompressedBytes += uncomp[ i ];
}
free( uncomp );
return true ;
}
/** Compress a STL string using zlib with given compression level and return
* the binary data. */
std::string compress_string(const std::string& str, int compressionlevel = Z_BEST_COMPRESSION)
{
z_stream zs; // z_stream is zlib's control structure
memset(&zs, 0, sizeof(zs));
if (deflateInit(&zs, compressionlevel) != Z_OK)
throw(std::runtime_error("deflateInit failed while compressing."));
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size(); // set the z_stream's input
int ret;
char outbuffer[32768];
std::string outstring;
// retrieve the compressed bytes blockwise
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = deflate(&zs, Z_FINISH);
if (outstring.size() < zs.total_out) {
// append the block to the output string
outstring.append(outbuffer,zs.total_out - outstring.size());
}
}
while (ret == Z_OK);
deflateEnd(&zs);
if (ret != Z_STREAM_END) { // an error occurred that was not EOF
std::ostringstream oss;
oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;
throw(std::runtime_error(oss.str()));
}
return outstring;
}
/* Reads a file into memory. */
bool loadBinaryFile( const std::string& filename, std::string& contents ) {
// Open the gzip file in binary mode
FILE* f = fopen( filename.c_str(), "rb" );
if ( f == NULL )
return false ;
// Clear existing bytes in output vector
contents.clear();
// Read all the bytes in the file
int c = fgetc( f );
while ( c != EOF ) {
contents += (char) c ;
c = fgetc( f );
}
fclose (f);
return true ;
}
int main(int argc, char *argv[]) {
// Read the gzip file data into memory
std::string fileData ;
if ( !loadBinaryFile( "myfilein.gz", fileData ) ) {
printf( "Error loading input file." );
return 0 ;
}
// Uncompress the file data
std::string data ;
if ( !gzipInflate( fileData, data ) ) {
printf( "Error decompressing file." );
return 0 ;
}
// Print the data
printf( "Data: "" );
for ( size_t i=0; i<data.size(); ++i ) {
printf( "%c", data[i] );
}
printf ( ""n" );
std::string outy;
// Compress the file data
outy = compress_string(data, 0);
//Write the gzipped data to a file.
FILE *handleWrite=fopen("myfileout.gz","wb");
fwrite(outy,sizeof(outy),1,handleWrite);
fclose(handleWrite);
return 0;
}
std::string
不能被强制转换为void*
(fwrite
所期望的)。
试题:
fwrite(outy.data(),outy.size(),1,handleWrite);
虽然我不确定是否存储二进制数据在std::string
是一个好主意。
相关文章:
- 树莓上的 Libtorch 无法加载 pt 文件,但在 ubuntu 上工作
- 我的文件无法正常工作,无法从C++文件中读取上下文
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作
- 有没有办法关闭文件? fclose 不能很好地工作
- G++ 在生成文件中使用时停止工作
- 代码作为 C 文件工作,但不作为C++文件,错误:'__builtin_types_compatible_p'未在此范围内声明
- 无法使用 Visual Studio 启动 DirectX 11 可执行文件,生成工作正常
- 使用.cpp文件让 RcppArmadillo 在 Rstudio 中工作
- 读取二进制文件大小的短自上周以来停止工作
- 我的 PowerShell 脚本无法按预期工作(用于编译C++文件)
- 不同文件的类,如何使其工作?
- Makefile在Linux中工作,但在Windows下不起作用,在子目录中找不到文件
- C Visual Studio代码不使用工作区中的单个文件夹编译
- 在循环工作时,首先将两个文件读为向量,但仅次于迭代一次
- 在Qt中使用工作线程将数据写入文件的正确方法是什么?
- OpenCV:文件存储操作员">>"在一个项目中工作,但不在另一个项目中工作(在同一类上)
- 仅一个功能的链接错误.在同一类和CPP文件中具有相似签名的其他函数工作正常
- 程序在我的C++工作区中找不到头文件
- 从工作目录 QtWebEngine 读取文件
- 不完整的参数错误,包括其他.cpp文件中的postgres.cpp工作文件