I am writing a program to compress a file consisting of hexadecimal values into run-length code. For example, given a file:
46 6f 6f 20 62 61 72 21 21 21 20 20 20 20 20
my code should produce the following file:
01 46 02 6f 01 20 01 62 01 61 01 72 03 21 05 20
I don't know why the program I've written gets stuck. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
int main(void){
int a, b, c, count=1, flag=TRUE;
FILE *file;
FILE *out;
file = fopen("input.txt", "r");
if(file){
out = fopen("input.txt.out", "a");
fscanf(file, "%x", &a);
while(fscanf(file, "%x", &c)){
while(flag==TRUE){
if(c==a){
count= count+1;
}else{
flag = FALSE;
}
b=a;
a=c;
}
fprintf(out, "%d %02x ", count, b);
count = 1;
flag = TRUE;
}
}else{
printf("ERROR: file not found.");
}
}
EDIT: I updated the code removing the !feof(file) argument and replacing it with an actual I/O function instead. Thanks for the insight. However, my program still doesn't work.