see my code is
#include <stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
int main(int argc, char**argv) {
unsigned char *message = NULL;
struct stat stp = { 0 };
stat("huffman.txt", &stp);
/*determine the size of data which is in file*/
int filesize = stp.st_size;
printf("\nFile size id %d\n", filesize);
FILE* original_fileptr = fopen("huffman.txt", "r");
message = malloc(filesize);
fread(message, 1, filesize, original_fileptr);
printf("\n\tEntered Message for Encode is %s= and length %d", message,strlen(message));
return 0;
}
here huffman.txt has size 20 bytes and following character are there
άSUä5Ñ®qøá”F„œ
output of this code is
File size id 20
Entered Message for Encode is άSUä5Ñ®qøá"F„œ= and length 21
now question is if size is 20 then why length is 21 ?
Because C doesn’t have native strings, only arrays of characters, and there is a hidden, ubiquitous assumption that the last array member is a zero.
Since you violate that assumption by reading only 20 bytes into an array of 20 elements, with no regard to whether the final byte is zero, and then using string features like
%sandstrlen, you get essentially undefined behavior.Getting an answer of 21 is pure luck; anything (far worse) could have happened.
Correct code could be something like this (assuming the file is a text file):
If you’re reading arbitrary (“binary”) files, this will generally not produce the expected result (unless you know what to expect).