-1

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).

7
  • Opening a file in "binary mode" simply means that when you read from it you will get raw data with no special treatment. If you output it it will output as-is, which will look like garbage. If you want it to output as 0s and 1s you'll need to write some code to do that. Commented May 27, 2016 at 21:53
  • 2
    @Wajid You are reading it as binary correctly. The problem is that there is no binary primitive data type in C/C++, so it's usually stored as characters (8 bits per character). You're telling the runtime to print that binary data as if it was a string, so you're getting the symbols because that just happens to be the string representation of the binary data you're printing. See this: Convert binary to string Commented May 27, 2016 at 21:54
  • If you want a job done right, you have to do it yourself. printf does 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. Commented May 27, 2016 at 21:56
  • 1
    You're not trying to write binary. Your specifier to printf says 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. Commented May 27, 2016 at 21:57
  • 1
    Do you want a good C or C++ answer? Commented May 28, 2016 at 0:34

3 Answers 3

3
 while(sizeForB = fread(ForBinary, 1, BUFSIZ, ReadBFrom)){             
           printf("%s", ForBinary);  }

This is wrong on many levels. First of all you said it is binary file - which means there might not be text in it in the first place, and you are using %s format specifier which is used to print null terminated strings. Again since this is binary file, and there might not be text in it in the first place, %s is the wrong format specifier to use. And even if there was text inside this file, you are not sure that fread would read a "complete" null terminated string that you could pass to printf with format specifier %s.

What you may want to do is, read each byte form a file, convert it to a binary representation (google how to convert integer to binary string say, e.g., here), and print binary representation for each that byte.

Basically pseudocode:

foreach (byte b in FileContents)
{
  string s = convertToBinary(b);
  println(s);
}
Sign up to request clarification or add additional context in comments.

Comments

1

How to view files in binary in the terminal?

Either

"hexdump -C yourfile.bin" perhaps, unless you want to edit it of course. Most linux distros have hexdump by default (but obviously not all).

or

xxd -b file

Comments

1

To simply read a file and print it in binary (ones and zeros), read it one char at a time. Then for each bit, print a '0' or '1'. Can print Most or Least significant bit first. Suggest MSb.

if (ReadBFrom) {
  int ch;
  while ((ch = fgetc(ReadBFrom)) != EOF) {
    unsigned mask = 1u << (CHAR_BIT - 1);  // CHAR_BIT is typically 8
    while (mask) {
       putchar(mask & ch ? '1' : '0');
       mask >>= 1;
    }
  }
  fclose(ReadBFrom);
}

1 Comment

your solution appears to work just perfect, it do print it in 1 and 0, now if i want to grab those binary number and put it in a txt file as a text and read it back using fread() from that file then write as a new file's binary data is it going to work. and many many thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.