c ++中boost :: iostreams :: gzip_decompressor的奇怪错误 [英] strange error with boost::iostreams::gzip_decompressor in c++

查看:336
本文介绍了c ++中boost :: iostreams :: gzip_decompressor的奇怪错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我正在开发使用c ++中的boost进行压缩和解压缩的方法.

I am developing to compress and decompress with boost in c++.

我的代码如下:

#include <stdio.h>
#include <vector>
#include <string>

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>

char *txtFile   = "D:/Temp/plainTest.txt";
char *txtFile2  = "D:/Temp/plainTest_.txt";
char *binFile   = "D:/Temp/plainTest.bin";

// compress
std::ifstream inStream(txtFile, std::ios_base::in);
std::ofstream outStream(binFile, std::ios_base::out);
boost::iostreams::filtering_streambuf< boost::iostreams::input> in;
in.push( boost::iostreams::gzip_compressor());
in.push( inStream );
boost::iostreams::copy(in, outStream);

// decompress
std::ifstream inStream2(binFile, std::ios_base::in);
std::ofstream outStream2(txtFile2, std::ios_base::out);
boost::iostreams::filtering_streambuf< boost::iostreams::input> in2;
in2.push( boost::iostreams::gzip_decompressor());
in2.push( inStream2 );
boost::iostreams::copy(in2, outStream2);   --->   this line gives me the following error

plainText.txt文件包含这些字符.

plainText.txt file contains those characters.

The following line is in error

std::ofstream ofs
The compiler would've spit out a warning (at least) if your file was called jest; but 

这给我一个错误.

First-chance exception at 0x75692EEC in C_test.exe: Microsoft C++ exception: boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::iostreams::zlib_error> > at memory location 0x00D8E074.

If there is a handler for this exception, the program may be safely continued.

0x75692EEC处的内存就像

And memory at 0x75692EEC is like

0x75692EEC  8b 4c 24 54 33 cc e8 9d a2 ff ff 8b e5 5d c2 10 00 8b 45 10 83 f8 0f 77 18 89 44 24 10 c1 e0 02 50 51 8d 44 24 1c 50 e8 c9 ad ff ff 83 c4 0c eb c5 6a 0f 58 eb e3 90 90 90 90 90 8b ff 55  ?L$T3????..??]?..?E.??.w.?D$.??.PQ?D$.P???..??.??j.X????????.U

但是有趣的是,如果我从plainText.txt中删除 ofs ,例如

But the funny thing is, if I delete ofs from the plainText.txt like

The following line is in error

std::ofstream
The compiler would've spit out a warning (at least) if your file was called jest; but 

错误消失了,它将产生plainText2.txt完美的文本.

The error goes away, and it will produce plainText2.txt perfect.

我做错了什么?

编辑,我指出该行给了我错误.

EDIT I pointed the line gives me the error.

推荐答案

我认为您需要指定输出流是二进制的:

I think you need to specify that the output stream is binary:

std::ofstream outStream(binFile, std::ios_base::binary);

然后,在写完之后,执行outStream.close()(以确保压缩的输出已刷新;您可以调用flush()代替,但是close()更确定能在所有文件系统上工作).然后,也打开文件以使用binary进行读取.请注意,无需指定std::ios_base::in(out),因为它是std :: ifstream(ofstream)的默认设置.

Then, after writing, do outStream.close() (to make sure the compressed output is flushed; you could call flush() instead but close() is more certain to work on all filesystems). Then, open the file for reading with binary as well. Note there is no need to specify std::ios_base::in (out), as it is the default for std::ifstream (ofstream).

这篇关于c ++中boost :: iostreams :: gzip_decompressor的奇怪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆