Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

c++ binary files

  • 04-04-2006 06:19PM
    #1
    Registered Users, Registered Users 2 Posts: 1,999 ✭✭✭


    I'm trying to make a simple rle compression program. I've inputed a text file which works fine but when I try to do the same with a bitmap it doesn't work.

    I'm storing the input from the file as type char and trying to compare them using ">, <, ==". If it matches I count the number of times it does then output the variable(char), then a count(int). This all goes into another binary file.

    I'm not sure if I'm explaining this properly so here's the code which should explain it better.
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    typedef vector<char> CompVec;
    CompVec compress;
    
    ifstream::pos_type size;
    char * memblock;
    char * compress_memblock;
    
    int i = 0;
    
    void addToCompressVector( char c_in )
    {
    	compress.push_back(c_in);
    }
    void addToCompressVector( int c_in)
    {
    	compress.push_back(c_in);
    }
    
    int fillMemory()
    {
    	//ifstream fileIn ("test.txt", ios::in|ios::binary|ios::ate);
    	ifstream fileIn ("test.bmp", ios::in|ios::binary|ios::ate);
    
    	if( fileIn.is_open() )
    	{
    		size = fileIn.tellg();
    
    		//debug
    		cout << "input file size: " << size << "bytes" << endl;
    
    		memblock = new char [size];
    		fileIn.seekg (0, ios::beg);
    		fileIn.read (memblock, size);
    		fileIn.close();
    
    		int num = size;
    		return num;
    	}
    	else
    	{
    		return -1;
    	}
    }
    
    char getc()
    {
    	if( i > ((int)size - 1))
    	{
    		return NULL;
    	}
    	else
    	{
    		char getChar;
    		getChar = memblock[i];
    		i++;
    		
    		return getChar;
    	}
    }
    
    void rleCompress()
    {
        char last = -1;
        char lastbo = -2;
    	char c;
    
    	int num = fillMemory();
    	if ( num != -1 )
    	{
    		while((c = getc()) != NULL)
    		{
    			addToCompressVector(c);
    
    			if (lastbo == last)
    			{
    				if (c == last)
    				{
    					int count = 0;
    
    					while (( c = getc()) != NULL )
    					{
    						if (c == last)
    						{
    							count++;
    						}
    						else
    						{
    							break;
    						}
    					}
    
    					if( count >= 0 )
    					{
    						addToCompressVector(count);
    					}
    
    					if( c >= 0 )
    					{
    						addToCompressVector(c);
    					}
    				}
    			}
    			lastbo = last;
    			last = c;
    		}
    
    	}
    	else
    	{
    		cout << "unable to open file"<< endl;
    	}
    }
    
    void outputCompress()
    {
    	ofstream fileOut ("test.dat");
    
    	if (fileOut.is_open())
    	{
    		int compress_memblock_size = compress.size();
    		compress_memblock = new char[compress_memblock_size];
    		for( int loop_i = 0; loop_i < compress_memblock_size; loop_i ++ )
    		{
    			compress_memblock[loop_i] = compress.at(loop_i);
    		}
    		fileOut.write( compress_memblock, compress_memblock_size );
    		fileOut.close();
    
    		delete[] memblock;
    	}
    	else
    	{
    		cout << "unable to open file"<< endl;
    	}
    }
    
    int main ()
    {
    	cout << "run rleCompress()" << endl;
    	rleCompress();
    	cout << "\nrun outputCompress()" << endl;
    	outputCompress();
    
    	cin.get();
    	return 0;
    }
    

    When I run this with the bitmap as the input file it will only loop 5 times then break when it shouldn't(I think)

    Any ideas where its messing up on me?


Comments

  • Registered Users, Registered Users 2 Posts: 1,275 ✭✭✭bpmurray


    while((c = getc()) != NULL)

    What happens if you have a byte in the file that is actually 0x00? This is something that isn't just likely: it's inevitable!

    Maybe a better way would be to return a 16-bit value from the getc function, and setting the high bit if you've hit EOF.


Advertisement