Hi..

I want to compress a text file which is like this:(input.txt)

ffa11 ffb13 ffd2 e c e r

and save the result in (output.txt)

#include<fstream>
#include<iostream>

using namespace std;

int main()
{
	char ch;
	int counter=0;

	fstream infile;
	fstream outfile;

	infile.open( "input.txt",ios::in );
	outfile.open( "output.txt",ios::app );

	infile.unsetf( ios::skipws );

	do
	{
		infile>>ch;
		
		if( ch=='f' )
		{
			infile>>ch;
			infile>>ch;
			infile>>counter;

			for( int i=counter; i<0; i-- )
			{
				outfile<<ch;
			}
			
		}
		else
		{
			outfile<<ch;
		}


	}while( !infile.fail() );

	outfile<<endl;

	return 0;

}

the problem is its not printing the a's, b's, d's
please help???

Recommended Answers

All 3 Replies

Firstly, write your code such that it terminates cleanly. You're loop ends when the program fails to read from infile, but your program will do calculations with the bad data that it tries to read. You need to rewrite your loop so that you check the state of the file before reading from it. Also, you I don't believe you can read integers directly from a text file (infile >> counter). You must first read the data as a string, then convert it to an integer. Your program also confuses me, as I don't see how it's compressing data. We'd be better equipped to help you if you could add comments and explain what you're attempting to do procedurally.

ok..

but can u tell me how to convert a string to an int?????

please^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.