当前位置:网站首页>C语言压缩字符串保存到二进制文件,从二进制文件读取压缩字符串后解压。
C语言压缩字符串保存到二进制文件,从二进制文件读取压缩字符串后解压。
2022-06-13 01:52:00 【Xeon_CC】
在vcpkg的环境下,确认已安装boost库
在Debug,x64环境下附加依赖项:


切换到Release,x64环境,并附加依赖项:

附加依赖项以后点击应用,程序跑起来就不会编译不通过。编写代码:
#include <iostream>
#include <sstream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <stdio.h>
#include <string>
using namespace std;
std::string compress(const std::string& data) {
namespace bio = boost::iostreams;
std::stringstream compressed;
std::stringstream origin(data);
bio::filtering_streambuf<bio::input> out;
out.push(bio::gzip_compressor(bio::gzip_params(bio::gzip::best_speed)));
out.push(origin);
bio::copy(out, compressed);
return compressed.str();
}
std::string decompress(const std::string& data)
{
namespace bio = boost::iostreams;
std::stringstream compressed(data);
std::stringstream decompressed;
bio::filtering_streambuf<bio::input> out;
out.push(bio::gzip_decompressor());
out.push(compressed);
bio::copy(out, decompressed);
return decompressed.str();
}
int main() {
// 先在a.bin文件手动输入一些字符串
//以二进制形式读取a.bin文件
FILE* raf = fopen("../static/a.bin", "rb");
char bufa;
string astr = "";
while (true) {
bufa = fgetc(raf);
if (feof(raf) != 1) {
astr += bufa;
}
else {
break;
}
}
fclose(raf);
//以二进制形式写入b.bin文件,并将从a.bin文件读取的字符串压缩后写入到b.bin文件
FILE* wbf = fopen("../static/b.bin", "wb");
string bstr = compress(astr);
for (int i = 0; i < bstr.size(); i++) {
fputc(bstr[i], wbf);
}
fclose(wbf);
//以二进制形式读取b.bin文件
FILE* rbf = fopen("../static/b.bin", "rb");
string readbstr = "";
char bufb;
while (true) {
bufb = fgetc(rbf);
if (feof(rbf) != 1) {
readbstr += bufb;
}
else {
break;
}
}
fclose(rbf);
//检查从bin文件读取的字符串和使用compress函数压缩的字符串是否相同。
//如果不采用二进制读取,直接读取的话,两者可能会不相同。
if (bstr == readbstr) {
cout << "same....." << endl;
}
//读取压缩的二进制文件并解压读取。
cout << "读取的压缩字符串::" << readbstr << endl;
cout << "decompressing..." << endl;
string decmpstr = decompress(readbstr);
cout << "解压后的字符串::" << decmpstr << endl;
return 0;
}
- 运行结果

查看a.bin文件,这是我随便从键盘输入的内容:
查看b.bin文件,这是压缩后的内容,在终端输出查看是乱码来的
边栏推荐
- Introduction to common ROS commands
- Use koa to mock data and set cross domain issues
- Machine learning basic SVM (support vector machine)
- Detailed explanation of audience characteristics
- Compiling minicom-2.7.1 under msys2
- [andoid][step pit]cts 11_ Testbootclasspathandsystemserverclasspath at the beginning of R3_ Analysis of nonduplicateclasses fail
- Plumber game
- Opencv camera calibration (2): fish eye camera calibration
- Use of Arduino series pressure sensors and detected data displayed by OLED (detailed tutorial)
- About the proposed signature file migration to industry standard format pkcs12
猜你喜欢

Should the audience choose observation mode or positioning mode?

Detailed explanation of maxpooling corresponding to conv1d, conv2d and conv3d machines of tensorflow2

6、 Implementation of warehouse out management function

【MathType】利用MathType输出LaTex样式的公式

白噪声的详细理解
![[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)](/img/f7/b2463d8ffe75113d352cae332046db.jpg)
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)

Server installation jupyterab and remote login configuration

Restful interface specification annotation of pringboot (2)

leetcode743. 网络延迟时间(中等, dijkstra)

Calculation of accuracy, recall rate, F1 value and accuracy rate of pytorch prediction results (simple implementation)
随机推荐
[arithmetic, relation, logic, bit, compound assignment, self increasing, self decreasing and other] operators (learning note 4 -- C language operators)
About constructive code blocks, static code blocks, and constructor execution order
STM32 3*3矩阵按键(寄存器版本)
MySQL - use field alias after where
TensorFlow2的Conv1D, Conv2D,Conv3D机器对应的MaxPooling详解
Jeux de plombiers
Examples of using the chromium base library
About tkinter Canvas does not display pictures
Devaxpress Chinese description --tcximagelist (enhanced image list control)
leetcode743. 网络延迟时间(中等, dijkstra)
4、 Improvement of warehousing management function
[MathType] use MathType to output latex style formula
Padavan mounts SMB sharing and compiles ffmpeg
Explanation and application of prefix sum (one-dimensional, two-dimensional)
Get started quickly cmake
六、出库管理功能的实现
Pyflink implements custom sourcefunction
Spit bubbles (stack)
什么是立体角
MySQL ---- where后使用字段别名