Search code examples
cbinaryfilesfread

How to read binary inputs from a file in C


What I need to do is to read binary inputs from a file. The inputs are for example (binary dump),

00000000 00001010 00000100 00000001 10000101 00000001 00101100 00001000 00111000 00000011 10010011 00000101

What I did is,

char* filename = vargs[1];

BYTE buffer;

FILE *file_ptr = fopen(filename,"rb");


fseek(file_ptr, 0, SEEK_END); 

size_t file_length = ftell(file_ptr);

rewind(file_ptr);  

for (int i = 0; i < file_length; i++)
{
   fread(&buffer, 1, 1, file_ptr); // read 1 byte
   printf("%d ", (int)buffer);
}

But the problem here is that, I need to divide those binary inputs in some ways so that I can use it as a command (e.g. 101 in the input is to add two numbers)

But when I run the program with the code I wrote, this provides me an output like:

0 0 10 4 1 133 1 44 8 56 3 147 6

which shows in ASCII numbers.

How can I read the inputs as binary numbers, not ASCII numbers?

The inputs should be used in this way:

0 # Padding for the whole file! 
0000|0000 # Function 0 with 0 arguments 
00000101|00|000|01|000 # MOVE the value 5 to register 0 (000 is MOV function)
00000011|00|001|01|000 # MOVE the value 3 to register 1 
000|01|001|01|100 # ADD registers 0 and 1 (100 is ADD function)
000|01|0000011|10|000 # MOVE register 0 to 0x03 
0000011|10|010 # POP the value at 0x03 
011 # Return from the function 
00000110 # 6 instructions in this function

I am trying to implement some sort of like assembly language commands

Can someone please help me out with this problem?

Thanks!


Solution

  • The format you use is not divisible by a byte, so you need to read your bits into a circular buffer and parse it with a state machine.

    Read "in binary" or "in text" is quite the same thing, the only thing that change is your interpretation of the data. In your exemple you are reading a byte, and you are printing the decimal value of that byte. But you want to print the bit of that char, to do that you just need to use binary operator of C.

    For example:

    #include <stdio.h>
    #include <stdbool.h>
    #include <string.h>
    #include <limits.h>
    
    struct binary_circle_buffer {
        size_t i;
        unsigned char buffer;
    };
    
    bool read_bit(struct binary_circle_buffer *bcn, FILE *file, bool *bit) {
        if (bcn->i == CHAR_BIT) {
            size_t ret = fread(&bcn->buffer, sizeof bcn->buffer, 1, file);
            if (!ret) {
                return false;
            }
            bcn->i = 0;
        }
        *bit = bcn->buffer & ((unsigned char)1 << bcn->i++); // maybe wrong order you should test yourself
        // *bit = bcn->buffer & (((unsigned char)UCHAR_MAX / 2 + 1) >> bcn->i++);
        return true;
    }
    
    int main(void)
    {
        struct binary_circle_buffer bcn = { .i = CHAR_BIT };
    
        FILE *file = stdin; // replace by your file
        bool bit;
        size_t i = 0;
        while (read_bit(&bcn, file, &bit)) {
            // here you must code your state machine to parse instruction gl & hf
            printf(bit ? "1" : "0");
            if (++i >= 7) {
                i = 0;
                printf(" ");
            }
        }
    }
    

    Help you more would be difficult, you are basically asking help to code a virtual machine...