I have a problem with reading binary file in C++. Currently my code is like this:
FILE *s=fopen(source, "rb");
fseek(s,0,SEEK_END);
size_file size=ftell(s);
rewind(s);
char *sbuffer=(char *) malloc(sizeof(char) * size);
if(sbuffer==NULL){
fputs("Memory error", stderr);
exit(2);
}
size_t result=fread(sbuffer,1,size,s);
if(result != size){
fputs("Reading error",stderr);
exit(3);
}
fclose(s);
cout<<sbuffer<<endl;
However, the characters printed out on the terminal are all random characters instead of what I write in the PDF file. They are like:
% P D F - 1 . 3
% ? ? ? ? ? ? ? ? ? ? ?
4 0 o b j
< < / L e n g t h 5 0 R / F i l t e r / F l a t e D e c o d e > >
s t r e a m
x ? ? ? j ? 0 E ? ? ? k ? y Q E # ? ? ? m ? & ? ? @ % + ? . ? ? ? ? A i ? 4 z \ 1 G W ? ? - , ? ? ? ( ? ? ? 9 ? ? ? ? ? \ ? } ? ? ? e ? ? ? ? 0 ? ? ? ~ ? , ? ? & 8 ? ? x e 4 ? r
| ? ? ?
? ? ? ? E > a ? ? z & ? Z ? < ? } ' ? ? ? j p ? ? Q 7 0 ? ? ? S % - p ? ? ? 7 D ? ? ? ' Q z Q ? ? ? ? ? ? ? ? ? ? \ 2 ? ? 7 ? ? ? < ? ? D ~ ? ? ?
e n d s t r e a m
e n d o b j
5 0 o b j
2 2 8
e n d o b j
2 0 o b j
And many others characters like the above. I tried to search for a long time but cannot find out how to get the actual characters out for later processing. By the way, I'm trying to write a compressor which takes binary file as input and output. Any help here is highly appreciated!