In: Computer Science
C Programming Run Length Encoder (Compression). I am writing a program that takes an image (from a text file) and looks for patterns of 2 or more duplicate values.
The program should replace these values with the following pattern: 2 such characters followed by an Integer ( which represents number of occurrences of each character), followed by an asterisk.
For example say the input of the non-compressed image was: ,,,,)H 7. i.e. it consists of four commas, one closed bracket, the letter H, three spaces, the number seven and a full stop.
The latter would be compressed into another text file as: ,,4*)H 3*7.
Here is the code I have so far which opens a image text file and displays ASCII image to the console. I am stuck on how I go about compressing this image into another file.
Any help that can help me get a little closer to writing this program will be much appreciated. Any clarifications feel free to comment. Thanks in advanced.
#include
void print_image(FILE *fptr);
int main(void)
{
    char *filename = "image.txt";
    FILE *fptr = NULL;
    if((fptr = fopen(filename,"r")) ==
NULL)
    {
        fprintf(stderr,"error
opening %s\n",filename);
        return 1;
    }
    print_image(fptr);
    fclose(fptr);
    return 0;
}
void print_image(FILE *fptr)
{
    char read_string[128];
    while(fgets(read_string,sizeof(read_string),fptr)
!= NULL)
{
        printf("%s",read_string);
}
}
/*
Method compress_file() accepts the file pointer for
image.txt.
Steps followed in method
1. Create string array with contents.
2. Parse the array to find pattern.
3. Replace the pattern as per requirement.
4. Write updated string array in new file
*/
void compress_file(FILE *fptr);
void compress_file(FILE *fptr)
{
    fseek(fptr, 0,
SEEK_END);    //move pointer to end of
file
    int len =
ftell(fptr);        //get
total length of file
    char
read_string[len];        //create
string array of size len
    while(fgets(read_string),
sizeof(read_string), fptr != NULL){}    //copy
file contents in read_string
    int i=0, count=0;
    while (string[i] != '\0')
{    //iterate array
        count =
1;    //to track pointer if there are no same
characters
        if(string[i]
==string[i+1]){    //check if 2 or more
characters are same
            count
= 2;
            for(int
j= i+1; string[j]!= '\0'; j++){    //check
further in array if there are similar characters
                if(string[j]!=string[j+1]){
                    break;
                }
                count++;    //increament
count if characters are same
            }
            string[i+count-1]=count
+ '0'; //convert int count to char and add in array
        }
        i = i + count;
//increament i
    }
    
    FILE *targetfptr =
fopen("compressed_file.txt","w");    //open
file in write mode
    if (targetfptr != NULL) {
        for (i = 0; i <
len; i++) {    ////iterate array
            fputc
(string[i], targetfptr);    //add character in
file
        }
        fclose(targetfptr);    //close
file
   }
}