Read file byte by byte using C

16,898

There a multiple problems in your code:

  • You do not test for fopen failure, causing undefined behavior if the file does not exist or cannot be open.
  • You are not reading the file byte by byte, instead you are reading blocks of 4900 bytes and just print one byte of each block.
  • You probably hit the end of file very quickly, but you do not test for end of file, so the output may come from the same part of the file. A classic case of one bug hiding another bug.
  • The conversion format %x outputs 1 or 2 characters per byte depending on the byte value. This is a problem is the file contents are 0x00, 0x01, 0x10 and 0x11, which respectively produce 0, 1, 10 and 11, hence the apparent misaligned input.
  • For cleanliness, you should fclose the stream.
  • From the data posted, it is unclear if the hex editor is displaying data in hexadecimal or binary format and whether the values are word based or byte based.

Here is a simpler approach:

#include <stdio.h>

int main() {
    FILE *fp;
    int c, i, max;

    fp = fopen("tugasz.ksa", "rb");
    if (fp == NULL) {
        fprintf(stderr, "cannot open input file\n");
        return 1;
    }
    for (i = 0, max = 4900; i < max && (c = getc(fp)) != EOF; i++) {
        printf("%02x", c);
        if (i % 16 == 15)
            putchar('\n');  // 16 bytes per line
        else if (i % 2 == 1)
            putchar(' ');   // group bytes in pairs
    }
    if (i % 16 != 0)
        putchar('\n');  // output a newline if needed

    fclose(fp);
    return 0;
}
Share:
16,898

Related videos on Youtube

Neios Flame Stuxnet
Author by

Neios Flame Stuxnet

still learn up

Updated on June 04, 2022

Comments

  • Neios Flame Stuxnet
    Neios Flame Stuxnet almost 2 years

    I am trying to read a file byte by byte then print it out using C, but the output doesn't match the display of the hex editor.

    In hex editor the first 2 lines look like this:

    0000 0000 0000 0000 0000 0000 0000 0000
    0000 0000 0000 0111 1111 1000 0000 0000
    

    expected output:

    00000000000000000000000000000000
    00000000000001111111100000000000
    

    but when my code outputs this:

    00000000000000000000001111111100
    00000000000000000000000000000000
    

    here's my code:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        FILE *fp;
        unsigned char buffer[4900] = "";
        int y;
    
        y = 0;
        fp = fopen("tugasz.ksa", "rb");
    
        for (int x = 0; x < 4900; x++) {
            fread(buffer, 1, 4900, fp);
            printf("%x", buffer[x]);
        }
        return (0);
    }
    
    • Some programmer dude
      Some programmer dude almost 6 years
      I suggest you do some rubber duck debugging. And no, you're not reading your file "byte by byte".
    • Weather Vane
      Weather Vane almost 6 years
      Move fread above the loop. Make it y = fread(...) and then loop to y.
    • Neios Flame Stuxnet
      Neios Flame Stuxnet almost 6 years
      @Weather it give me more different output "1ddd3cc0"
    • Neios Flame Stuxnet
      Neios Flame Stuxnet almost 6 years
      @Weather wait i'll try it
    • Weather Vane
      Weather Vane almost 6 years
      Cannot reproduce, after making those changes it works as expected. Check fp after you open the file to make sure it really did open.
    • Neios Flame Stuxnet
      Neios Flame Stuxnet almost 6 years
      still not working :(
    • Idea Wang
      Idea Wang almost 6 years
      Is %x really the same 'hex' thing as your hex editor? I think you should try to use itoa or some other things to get the right output.
    • Jonathan Leffler
      Jonathan Leffler almost 6 years
      You're not checking that the file is opened successfully. You're not checking the return value from fread(). When you read, you attempt to read 4900 bytes at a go (not byte by byte as your question title implies). You print one byte from each of the 4900 values you 'read'. Use getc() to read bytes one at a time. Or use fread() but heed the return value (it says how many bytes were read) and then loop over the values read. The outer loop should call fread() and capture and test the result — size_t nbytes; while ((nbytes = fread(…)) > 0). An inner loop deals with nbytes of data.