Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

c++ binary files

  • 04-04-2006 5:19pm
    #1
    Registered Users, Registered Users 2 Posts: 1,997 ✭✭✭


    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