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

David C
  • 7,204
  • 5
  • 46
  • 65
Abdul
  • 63
  • 1
  • 10
  • 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. – kfsone May 27 '16 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](/questions/2343099/convert-binary-format-string-to-int-in-c) – Tibrogargan May 27 '16 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. – user3386109 May 27 '16 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. – Ken White May 27 '16 at 21:57
  • 1
    Do you want a good C or C++ answer? – chux - Reinstate Monica May 28 '16 at 00:34
  • so let me get this this straight what i exactly am trying to do is to read binary data and put as a text in a txt file and and then with other program i want to read it from txt file and put as binary data of a new file, and i do understand %s is strings and %d for decimal but i don't know what is for binary. – Abdul May 28 '16 at 07:53
  • is somebody there? – Abdul May 29 '16 at 07:59

3 Answers3

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);
}
Community
  • 1
  • 1
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
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

Community
  • 1
  • 1
djechlin
  • 59,258
  • 35
  • 162
  • 290
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);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 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 – Abdul May 28 '16 at 10:26