I try to read binary(executeable) files, butsome problem as it prints extra character(0xffffff) after every few bytes. But this code work perfectly for text file(Non-binary). The code is:
int main(int argc, char **argv) { uint64_t i=0; FILE * fp=fopen(argv[1],"rb"); uint8_t a; long int size=0; char *msg; size=findSize(argv[1]); msg=(char *)malloc(size+1); while((a=fgetc(fp))!=EOF) { printf("%02x",a); msg[i]=(char)a; i++; if(i==size) break; } i=0; for(i=0;i<size;i++) { printf("%x",msg[i]); } return 0; } When i try to print the value(s) of a, It works perfectly, while printing msg[i], it prints extra bytes i,e:
Orginal bytes: 0xa31c4b1ab66b900
Output with extra bytes: 0xa31c4ffffffb1ffffffafffffffb66ffffffb900
fgetc()returns an int value. So,ashould be defined as an int.EOFfrom0xFF.charto anintwhen passing toprintf. This include sign extensions for negative values.fgetcthing is relevant when you're working with binary fileslongmight not be sufficiently large to contain every file size, but it perhaps wouldn't be a problem.