Click here to Skip to main content
15,914,013 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
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
Posted
Updated 11-May-14 22:10pm
v3
Comments
Richard MacCutchan 12-May-14 5:24am    
And your problem is?

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900