huffman occurences works until last BUFSIZE block

This commit is contained in:
Christoph J. Scherr 2022-11-26 15:46:33 +01:00
parent e427fe4c0b
commit f0616d03fd
1 changed files with 21 additions and 3 deletions

View File

@ -56,6 +56,7 @@ off_t fsize(const char *filename) {
return -1; return -1;
} }
// print help
void helper() { void helper() {
printf("huffman compression algorithm implementation for educational " printf("huffman compression algorithm implementation for educational "
"purposes.\n\nSyntax:\nhuffman -f fileToCompress\t\tcompress the " "purposes.\n\nSyntax:\nhuffman -f fileToCompress\t\tcompress the "
@ -63,6 +64,7 @@ void helper() {
"file\nhuffman -h\t\t\t\tshow this help\nhuffman -v\t\t\t\tverbose\n"); "file\nhuffman -h\t\t\t\tshow this help\nhuffman -v\t\t\t\tverbose\n");
} }
// let the magic happen
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int opt; int opt;
bool extract_mode = false; bool extract_mode = false;
@ -156,7 +158,7 @@ int main(int argc, char *argv[]) {
} }
} }
initNodes(); initNodes();
int ret; size_t ret;
while(!feof(fptrR)){ while(!feof(fptrR)){
ret = fread(buf, 1, BUFSIZE, fptrR); ret = fread(buf, 1, BUFSIZE, fptrR);
if(ret == BUFSIZE){ if(ret == BUFSIZE){
@ -181,14 +183,30 @@ int main(int argc, char *argv[]) {
printf("Undefined behaviour while reading file.\n"); printf("Undefined behaviour while reading file.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
fseek(fptrR, BUFSIZE, SEEK_CUR); // fread seems to advance the File pointer by itself, so the following line would skip half of the file.
// FIXME, the last 512 bit block seems to be skipped.
// fseek(fptrR, BUFSIZE, SEEK_CUR);
} }
if(debug){ // show occurences
printf("Occurences (hex):\n");
size_t addedUpOccurences = 0;
for(int i = 0; i < 256; i++){
if(i%5==0)
printf("\n");
printf("0x%02x: 0x%016llx ", i, nodes[i].occurences);
addedUpOccurences += nodes[i].occurences;
}
printf("\nFilelength: %dB\nAdded up occurences: %d\nOccurences in Filelength: %f%%\n",
filelen, addedUpOccurences, (float)(100*(addedUpOccurences/(float)filelen)));
}
} }
fclose(fptrR); fclose(fptrR);
printf("\n"); printf("\n");
if(debug) // wait for input to end. if(debug){ // wait for input to end.
printf("Press Enter to finish.\n");
getchar(); getchar();
}
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }