Basically, I am trying to read binary data of a file by using fread() and print it on screen using printf(), now, the problem is that when it prints it out, it actually don't show it as binary 1 and 0 but printing symbols and stuff which I don't know what they are.
This is how I am doing it:
#include <stdio.h>
#include <windows.h>
int main(){
size_t sizeForB, sizeForT;
char ForBinary[BUFSIZ], ForText[BUFSIZ];
char RFB [] = "C:\\users\\(Unknown)\\Desktop\\hi.mp4" ; // Step 1
FILE *ReadBFrom = fopen(RFB , "rb" );
if(ReadBFrom == NULL){
printf("Following File were Not found: %s", RFB);
return -1;
} else {
printf("Following File were found: %s\n", RFB); // Step 2
while(sizeForB = fread(ForBinary, 1, BUFSIZ, ReadBFrom)){ // Step 1
printf("%s", ForBinary);
}
fclose(ReadBFrom);
}
return 0;
}
I would really appreciate if someone could help me out to read the actual binary data of a file as binary (0,1).
printfdoes not have a conversion specifier that outputs in binary. So you have to write a nested loop where the outer loop gets a character from the buffer, and the inner loop converts the character and outputs it as binary.printfsays you're passing it a string, which you're not. Don't be surprised when things don't go as expected when you tell it you're passing one thing and you then pass it totally different content. Printf can't tell ahead of time that what you're passing is different; it has to trust you as the developer to really pass the content you've said you're going to pass, and you're not being honest and passing that content.