当前位置:网站首页>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
边栏推荐
- The method of drawing rounded panel with Delphi
- [work notes] xr872 codec driver migration and application program example (with chip debugging method)
- What is Google plus large text ads? How to use it?
- When AI meets music, iFLYTEK music leads the industry reform with technology
- 传感器:SHT30温湿度传感器检测环境温湿度实验(底部附代码)
- Gome's ambition of "folding up" app
- 万字讲清 synchronized 和 ReentrantLock 实现并发中的锁
- [work notes] the problem of high leakage current in standby mode of dw7888 motor driver chip
- Using atexit to realize automatic destruct of singleton mode
- Devexpress implementation flow chart
猜你喜欢

Simple ranging using Arduino and ultrasonic sensors

What is the path field—— Competitive advertising

In addition to the full screen without holes under the screen, the Red Devils 7 series also has these black technologies

Mac使用Docker安装Oracle

Ctrip reshapes new Ctrip

STM32 IIC protocol controls pca9685 steering gear drive board
![[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys](/img/fc/f03c7dc4d5ee12aaa301f54e4cd3f4.jpg)
[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys

Use mediapipe+opencv to make a simple virtual keyboard

Build MySQL environment under mac

万字讲清 synchronized 和 ReentrantLock 实现并发中的锁
随机推荐
What did Hello travel do right for 500million users in five years?
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)
STM32 IIC protocol controls pca9685 steering gear drive board
Anti crawling strategy (IP proxy, setting random sleep time, bilbili video information crawling, obtaining real URLs, processing special characters, processing timestamp, and multithreading)
js获取元素
Pytoch freeze pre training weights (feature extraction and BN layer)
rsync 傳輸排除目錄
The method of drawing rounded panel with Delphi
Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)
3、 Upload fabric photos to SQL server and provide name to display fabric photos
Examples of using the chromium base library
Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
DFS and BFS to solve Treasure Island exploration
Can't use typedef yet? C language typedef detailed usage summary, a solution to your confusion. (learning note 2 -- typedef setting alias)
Padavan mounts SMB sharing and compiles ffmpeg
Simple ranging using Arduino and ultrasonic sensors
How many smart bids does Google have?
移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式
Application and routine of C language typedef struct
如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】