This task is probably the most tricky - We need to create an RLE file from an ascii art picture so we need look at the strings of data and convert for example, ******llll****** into 06*04l06*
After that we need to compare the number of characters in the original picture with the number we used to store it as RLE (hopefully there should be less characters in the RLE data than the original picture.
If you were to do this with a piece of paper and a pen you might look at the line and count each character that is the same as the previous one - until you arrive at a new type of character.
For example - 0000000000HHHHKKKKK - if you looked at this line you would count to 10 zeros and then come across an H - so your first thing to write down is 100 or 10 zeros.
I looked at the program in the same kind of way line by line.
Looking at the string the first thing we need to do is to see if one character matches the next one. So you could write that as if character1 == charater2 etc...
I did this with one character only as a definition I called checkMatch - it takes two parameters (i.e. two single characters) and returns true for a match and false otherwise.
The next thing I decided was that there needed to be some way of building an RLE string and making sure there was a 0 in front of single digit numbers so 9* becomes 09*
This definition takes three parameters - the character itself the number of times it appeared in a run and the string that represents the RLE line we are building.
If the numberCount - is less than 10 then it will be a single integer figure so we need to make sure there is a zero in front - so I have added a "0" to the front of the number. Then we make up the linestring with the count and the character. - so if this definition is sent
* 8 "02H22F
It will return a new string - "02H22F08* (adding the 08* onto the previous string of RLE)
The final bit to put that together (ignoring the file opening for now) I have put a lot of comments in to show how it is working.
At this stage we can make up a whole line of RLE data from an ASCii art picture. - We still need to do the final bits of storing in a new text file and comparing the character lengths of the new file and the original file.
Again I have put quite a few comments into the code to show how it works.
It goes in this order
- Open the file with the ASCii are in
- Put the contents into a variable called stringorig
- Close the file
- open the file again
- step through the file line by line
- make a line of RLE from each line - adding it to a new string (stringRLE)
- Close the file
- Make a new file - get a name from the user
- Write the contents of the new string (stringRLE) to the new file
- close the file
- Work out the difference in length between the orig file and the new file (compare the strings we made)
We have now finished all the tasks - testing you should be able to make a new file with RLE data in - showing that file created in your folder structure.
Try this file - it is ascii art of an elephant - see if you can RLE encode it