使用rle c ++解压缩文件

编程入门 行业动态 更新时间:2024-10-28 13:24:09
本文介绍了使用rle c ++解压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

// RLE algorithm.cpp : Defines the entry point for the console application. // #include "stdafx.h"//work with txt files only #include"iostream" #include "fstream" using namespace std; ///////// structures ////////////// void decompress(char* data, int count, FILE* outfile); char* readFileData(char* filename, int* count_ptr); struct compress { char runValue; int counter; compress(int x=0,int y=1):runValue(x),counter(y){}//constructor }; int _tmain(int argc, _TCHAR* argv[]) { fstream infile1,infile2,outfile; struct compress zip; char cur; char next; char fName[100]=""; cout<<"please enter file name :"; cin>>fName; infile1.open(fName,ios::in); infile1.unsetf(ios::skipws); infile2.open(fName,ios::in); infile2.unsetf(ios::skipws); outfile.open("compressed.txt",ios::out); outfile.unsetf(ios::skipws); while(1) { infile1>>cur; if(infile1.fail()) break; infile2>>next; infile2>>next; if(infile2.fail()) break; while(1) { if(cur!=next) { outfile<<"1"<<cur; // handled error infile1>>cur; infile2>>next; if(infile2.fail()) break; } if(cur==next) { while(cur==next) { zip.counter++; infile1>>cur; infile2>>next; if(infile2.fail()) break; } zip.runValue=cur; outfile<<zip.counter<<zip.runValue; zip.counter=1; infile1>>cur; infile2>>next; if(infile2.fail()) break; } }// end of first while }// end of file infile1.close(); infile2.close(); outfile.close(); cout<<"compression operion completed.\n"; return 0; } void decompress(char* data, int count, FILE* outfile) { // TODO: decompress the data instead of just writing it out to the file for (int i = 0; i<count;> { putc(data[i], outfile); // write out a single byte of data } } char* readFileData(char* filename, int* count_ptr) { // Returns a pointer to an array storing the file data. // Sets the variable pointed to by 'count' to contain the file size. // Exits the program if the filename doesn't exist. FILE* infile = fopen(filename, "rb.txt"); if (!infile) { printf("No such file \"%s\"!\n", filename); exit(1); } // Get file size by going to the end of the file, getting the // position, and then going back to the start of the file. fseek(infile, 0, SEEK_END); int count = ftell(infile); fseek(infile, 0, SEEK_SET); // read the data from the file char* data = new char[count]; fread(data, 1, count, infile); fclose(infile); *count_ptr = count; return data; }

这是代码,我需要将其解压缩

this is the code and i need to decompress it

推荐答案

算法创建一个包含4字节计数器后跟字符的记录的压缩文件。 文件看起来像这样 - iiiic iiiic iiiic iiiic iiic 要解压缩文件,请在5个字节处读取压缩文件一段时间,即读取 iiiic 。 将字符c写入输出文件iiii次。 循环执行此操作直至结束该文件。 The algorithm creates a compressed file containing records with a 4 byte counter followed by the character. The file would look something like this - iiiiciiiiciiiiciiiiciiic To decompress the file read the compressed file 5 bytes at a time, ie, read iiiic. Write the character c to the output file iiii times. Do this in a loop till the end of the file.

更多推荐

使用rle c ++解压缩文件

本文发布于:2023-10-30 01:13:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:解压缩   文件   rle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!