Hi all, I am currently doing an exercise in KNKING C program a modern approach. THis exerise

----------------------
Of the many techniques for compressing the contents of a file, one of the simplest and fastest is known as run-length encoding. This technique compresses a file by replacing sequences of identical bytes by a pair of bytes: a repetition count followed by a byte to be repeated. For example, suppose that the file to be compressed begins with the following sequence of bytes (shown in hexadecimal):


46 6F 6F 20 62 61 72 21 21 21 20 20 20 20 20


The compressed file will contain the following bytes:


01 46 02 6F 01 20 01 62 01 61 01 72 03 21 05 20


Run-length encoding works well if the original file contains many long sequences of identical bytes. In the worst case (a file with no repeated bytes), run-length encoding can actually double the length of the file.
----------------------------------

I have a question regarding my code related to the exercise, this part of the code (and a particular line in question)

Code:

Code:
    while ( fread(&value,sizeof(unsigned char),1,fp) > 0)
    {
        count = 1;
        position = ftell(fp);
        while ( fread(&next,sizeof(unsigned char),1,fp) > 0 && next == value)
        {
                count ++;
        }


        fwrite(&count,sizeof(unsigned char),1,fpout);
        fwrite(&value,sizeof(unsigned char),1,fpout);
        fseek(fp,-1L,SEEK_CUR);  (THIS LINE IN PARTICULAR)     }


with regards to "fseek(fp,-1L,SEEK_CUR);" , my rationale behind it was the program will keep reading the bytes until it has read the first byte that is different. It then moves back by one byte position hence the "-1L", so that on the next loop it will read back the byte. E.g.

01 01 01 01 02 02

It reads all the 01 until it reads the first 02, then fseek moves the file positon back by 1 byte so on the next iteration of the loop it will read the first 02 again. However, if I implement the code this way it doesnt work.

fseek(fpin, position + (amount - 1), SEEK_SET);
^ This works however. Positon is the position of the file after reading the first byte, and amount is the number of bytes already read. I understand how this particular line of code works, but I do not understand why my SEEK_CUR method doesnt work. Thank you all for the help really