CPP program to compress text file

CPP program to compress text file using C++ file handling. This is the third program in the series of compression utility that we are trying to build.

Other programs that we have already covered in this series are

  1. Remove multi-line comments from any text file ( C/C++ style comments )
  2. Remove single line comments from any text file ( C/ C++ style comments)

This program will replace all the occurrence of multiple spaces, tabs, new line with single space thus save a lots of space on the WebServer. These programs are specially very useful for the people developing web-applications using CSS, html, javaScripts and PhP.

This trick is basically used by web-developer to avoid unwanted changes from his/her source code and to enhance the revenue of their products.

The source code of compression program is as follows

/*	Program to replace multiple spaces/tab 					with		 a single space
	made by		: rakesh kumar
	file name	: multiple_space.cpp   					   file      in this    file
*/

#include
#include
using namespace std;
int main()
{
	ifstream fin("single.php");
	ofstream fout("compressed.php");
	char ch;
	int count=0;
	while(fin.get(ch))
	{
		if(ch=='\t'||ch=='\n'||ch=='\r')  
			ch =' ';
		if(ch==' ')
			count++;
		else
		   count=0;
		if(count<=1)	
		    fout<<ch;
	}
	fin.close();
	fout.close();
	cout<<"\n Compression complete...";
	return 0;
}

This program takes “single.php” file as its input and produce a new file “compressed.php”. This way we will able to save a lots of bandwidth as well as our programs will be hard for non-programmers to understand.

%d bloggers like this: