huffman occurence working without hack

This commit is contained in:
Christoph J. Scherr 2022-11-26 16:09:13 +01:00
parent f0616d03fd
commit 0a5d3e8160
1 changed files with 15 additions and 5 deletions

View File

@ -175,16 +175,24 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
else{ else{
// reached EOF, finished // reached EOF, but we might still have more Bytes in Buffer, as not every File has n*BUFSIZE Bytes in it.
break; /*
* I don't really get why i need to use ret+BUFSIZE, sounds to me like it produces
* a buffer overflow, but somehow it gets those last bytes i couldn't get before and brings
* Occurences to 100% in all cases compared against true filelength.
* TODO understand why this works!
*/
for(int i = 0; i < ret+BUFSIZE; i++){
nodes[buf[i]].occurences++;
}
} }
} }
else{ else{
printf("Undefined behaviour while reading file.\n"); printf("Undefined behaviour while reading file.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// FIXME, the last 512 bit block is always skipped.
// fread seems to advance the File pointer by itself, so the following line would skip half of the file. // 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); // fseek(fptrR, BUFSIZE, SEEK_CUR);
} }
@ -197,8 +205,10 @@ int main(int argc, char *argv[]) {
printf("0x%02x: 0x%016llx ", i, nodes[i].occurences); printf("0x%02x: 0x%016llx ", i, nodes[i].occurences);
addedUpOccurences += nodes[i].occurences; addedUpOccurences += nodes[i].occurences;
} }
printf("\nFilelength: %dB\nAdded up occurences: %d\nOccurences in Filelength: %f%%\n", printf("\nFilelength: %lldB\nAdded up occurences: %lld\nDifference: %lld\nOccurences in Filelength: %f%%\n",
filelen, addedUpOccurences, (float)(100*(addedUpOccurences/(float)filelen))); filelen, addedUpOccurences,
filelen - addedUpOccurences,
(float)(100*(addedUpOccurences/(float)filelen)));
} }
} }