当前位置:网站首页>C language compressed string is saved to binary file, and the compressed string is read from binary file and decompressed.
C language compressed string is saved to binary file, and the compressed string is read from binary file and decompressed.
2022-06-13 02:03:00 【Xeon_ CC】
stay vcpkg Under the environment of , Confirm that... Is installed boost library
stay Debug,x64 Additional dependencies in the environment :


Switch to Release,x64 Environmental Science , And attach dependencies :

After attaching the dependency, click apply , The program will not fail to compile when it runs .Write code :
#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() {
// First in a.bin File manually enter some strings
// Read in binary form a.bin file
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);
// Write... In binary form b.bin file , And will be taken from a.bin The string read from the file is compressed and written to b.bin file
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);
// Read in binary form b.bin file
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);
// Check from bin File read string and use compress Whether the string compressed by the function is the same .
// If binary reading is not used , If you read directly , The two may be different .
if (bstr == readbstr) {
cout << "same....." << endl;
}
// Read the compressed binary file and extract it .
cout << " Read compressed string ::" << readbstr << endl;
cout << "decompressing..." << endl;
string decmpstr = decompress(readbstr);
cout << " Decompressed string ::" << decmpstr << endl;
return 0;
}
- Running results

see a.bin file , This is what I casually input from the keyboard :
see b.bin file , This is the compressed content , The output of the terminal is garbled
边栏推荐
- Sqlserver2008 denied select permission on object'***** '(database'*****', schema'dbo')
- uniapp 预览功能
- Opencv camera calibration (2): fish eye camera calibration
- Devaxpress Chinese description -- tdxgallerycontrol object (gallery component)
- rsync 傳輸排除目錄
- [work notes] the problem of high leakage current in standby mode of dw7888 motor driver chip
- STM32 IIC protocol controls pca9685 steering gear drive board
- [work notes] xr872 codec driver migration and application program example (with chip debugging method)
- Devaxpress Chinese description --tdximageslider (picture rotation control)
- Differences between constants and variables (detailed description) (learning note 3 -- variables and constants)
猜你喜欢

Get started quickly cmake

STM32 sensorless brushless motor drive

Calculation of accuracy, recall rate, F1 value and accuracy rate of pytorch prediction results (simple implementation)

移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式

Stm32 mpu6050 servo pan tilt support follow

Viewing the ambition of Xiaodu technology from intelligent giant screen TV v86
![[the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer](/img/c4/0d97def5fb587b8301bcb907fc6fcf.jpg)
[the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer

指针链表的实现

【Unity】打包WebGL項目遇到的問題及解决記錄

Luzhengyao, who has entered the prefabricated vegetable track, still needs to stop being impatient
随机推荐
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)
Application and routine of C language typedef struct
Implementation of pointer linked list
Functional translation
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO
一、搭建django自动化平台(实现一键执行sql)
Decompression and compression of chrome resource file Pak
Restful interface specification annotation of pringboot (2)
Bluetooth module: use problem collection
TensorFlow 2. X multi graphics card distributed training
Shell command notes
Day 1 of the 10 day smart lock project (understand the SCM stm32f401ret6 and C language foundation)
华为设备配置CE双归属
STM32 sensorless brushless motor drive
Rsync transport exclusion directory
Delphi 10.4.2 release instructions and installation methods of three patches
Devexpress implementation flow chart
Vs how to enter chromium subprocess debugging
Sensor: sht30 temperature and humidity sensor testing ambient temperature and humidity experiment (code attached at the bottom)
In addition to the full screen without holes under the screen, the Red Devils 7 series also has these black technologies