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