当前位置:网站首页>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文件,这是压缩后的内容,在终端输出查看是乱码来的
边栏推荐
- How do you use your own data to achieve your marketing goals?
- Service creation and operation example of ROS
- Opencv camera calibration (2): fish eye camera calibration
- 一种不带CPU的DPU架构:Hyperion
- Matplotlib drawing Chinese garbled code
- 六、出库管理功能的实现
- [printf function and scanf function] (learning note 5 -- standard i/o function)
- Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)
- Record the VMware installation process of VMware Tools and some problems encountered
- 如何通过受众群体定位解决实际问题?
猜你喜欢

Server installation jupyterab and remote login configuration
![[wsl2]wsl2 migrate virtual disk file ext4 vhdx](/img/e9/4e08e07c2de2f99c2938e79f7f1c44.png)
[wsl2]wsl2 migrate virtual disk file ext4 vhdx

What is Google plus large text ads? How to use it?

Use of Arduino series pressure sensors and detected data displayed by OLED (detailed tutorial)

Implementation of pointer linked list

pringboot之restfull接口规范注解(二)

Magics 23.0如何激活和使用视图工具页的切片预览功能

About tkinter Canvas does not display pictures

STM32 3*3矩阵按键(寄存器版本)

水管工遊戲
随机推荐
Stone from another mountain: Web3 investment territory of a16z
Record the VMware installation process of VMware Tools and some problems encountered
关于tkinter.Canvas 不显示图片的问题
[soft test] software designer knowledge points sorting (to be updated)
Restrict cell input type and display format in CXGRID control
Day 1 of the 10 day smart lock project (understand the SCM stm32f401ret6 and C language foundation)
Server installation jupyterab and remote login configuration
[andoid][step pit]cts 11_ Testbootclasspathandsystemserverclasspath at the beginning of R3_ Analysis of nonduplicateclasses fail
开发者来稿|AMD赛灵思中文论坛分享 - 提问的智慧
rsync 传输排除目录
csdn涨薪技术之Jmeter接口测试数据库断言的实现与设计
Devaxpress Chinese description --tdximageslider (picture rotation control)
[arithmetic, relation, logic, bit, compound assignment, self increasing, self decreasing and other] operators (learning note 4 -- C language operators)
Delphi Google API text to speech MP3 file
About inquirerjs
Ten thousand words make it clear that synchronized and reentrantlock implement locks in concurrency
路径字段是什么? ——竞价广告
10 days based on stm32f401ret6 smart lock project practice day 1 (environment construction and new construction)
Devexpress implementation flow chart
redis