Look at this:
int count;
char Message[count];
unsigned CompressedSize = sizeof(Message)*7/8;
Creating an array on the stack with a size determined at runtime is a GNU extension. Which isn't to say that its wrong, but there are some things to be aware of:
1) if
count
is larger than maximum stack size, you can corrupt other variables already on the stack. It might be better to use
malloc/free
to create your Message variable.
2) The size of the Message variable is set at runtime, based on the size of the input file. However the
sizeof
operator is calculated at
compile time. Since the size of the array isn't known at compile time,
sizeof(Message)
evaluates to
sizeof(char *)
. This means that the value of CompressedSize is going to be wrong.
3) Its not portable. I think clang probably supports this, but I'm fairly sure that MSVC does not. That may or may not be an issue for you, either now or later.
As an aside, this code
while( (letters = fgetc(file)) != EOF) {
count++;
}
fseek(file, 0, SEEK_SET);
could be replaced with
fseek(file, 0, SEEK_END);
long count = ftell(file);
fseek(file, 0, SEEK_SET);
This way, you don't have to spend time reading each byte in the file, and you can save yourself a (small) amount of space, since the variable "letters" is no longer needed.