当前位置:网站首页>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文件,这是压缩后的内容,在终端输出查看是乱码来的
边栏推荐
- 四、入库管理功能的完善
- cin,cin. get(),cin. Summary of the use of getline() and getline()
- 三、上传织物图片至SQL Server并提供name进行展示织物照片
- 深度学习调参技巧详解
- CXGRID keeps the original display position after refreshing the data
- 水管工遊戲
- Implementation of pointer linked list
- [the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] GPIO and register
- VI keyboard diagram
- 兴趣相似的受众群体
猜你喜欢

Using OpenCV in go
![[wsl2]wsl2 migrate virtual disk file ext4 vhdx](/img/e9/4e08e07c2de2f99c2938e79f7f1c44.png)
[wsl2]wsl2 migrate virtual disk file ext4 vhdx

Day 1 of the 10 day smart lock project (understand the SCM stm32f401ret6 and C language foundation)

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

水管工游戏

微服务开发环境搭建

Qt实现思维导图功能(二)

What is solid angle

Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)

STM32 3*3矩阵按键(寄存器版本)
随机推荐
Combining strings and numbers using ssstream
VI keyboard diagram
Opencv camera calibration (2): fish eye camera calibration
Compiling minicom-2.7.1 under msys2
[MathType] use MathType to output latex style formula
matplotlib画图中文乱码
关于tkinter.Canvas 不显示图片的问题
A DPU architecture without CPU: Hyperion
Stm32 3*3 matrix key (register version)
Restrict cell input type and display format in CXGRID control
Using OpenCV in go
JSON and protobuf Any interchange
Devaxpress Chinese description -- tdxgallerycontrol object (gallery component)
What is Google plus large text ads? How to use it?
30: Kakfa simulates JSON data generation and transmission
D template instance does not match declaration
Workspace for ROS
Stone from another mountain: Web3 investment territory of a16z
Read routing table
Using atexit to realize automatic destruct of singleton mode