From 0a5d3e8160a7651ff91a7c1a37fe1bfa3c94351a Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 26 Nov 2022 16:09:13 +0100 Subject: [PATCH] huffman occurence working without hack --- huffman/huffman.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/huffman/huffman.c b/huffman/huffman.c index 6978dad..00bd631 100644 --- a/huffman/huffman.c +++ b/huffman/huffman.c @@ -175,16 +175,24 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } else{ - // reached EOF, finished - break; + // reached EOF, but we might still have more Bytes in Buffer, as not every File has n*BUFSIZE Bytes in it. + /* + * 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{ printf("Undefined behaviour while reading file.\n"); 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. - // FIXME, the last 512 bit block seems to be skipped. // fseek(fptrR, BUFSIZE, SEEK_CUR); } @@ -197,8 +205,10 @@ int main(int argc, char *argv[]) { 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))); + printf("\nFilelength: %lldB\nAdded up occurences: %lld\nDifference: %lld\nOccurences in Filelength: %f%%\n", + filelen, addedUpOccurences, + filelen - addedUpOccurences, + (float)(100*(addedUpOccurences/(float)filelen))); } }