使用rle c ++解压缩文件 [英] decompress file using rle c++

查看:73
本文介绍了使用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 ++解压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆