Read data from a file in C

139 Views Asked by At

I wrote below data (this is the hex form of char* data) in a file in c programming.

58 c8 c3 f7 41 22 b1 72 9f 41 50 37 cb 09 d0 11 1d 5a 48 59 96 11 fa 4b fd d5 86 70 18 2c 50 2f fd 67 71 3d 20 fc 17 e1 27 f7 9c be 03 74 74 56 6c 49 e9 ee 24 9d 0a 06 da 6a 80 20 4d 91 e9 00 a2 ef ae db 1b 5d 39 9c a0 fe 0a 68 4e 0f 37 08 71 0f 15 a0 1a 32 e7 e6 69 53 aa ad 1e 07 8f 10

After I read this file with below code, it reads only first 63 byte (until 00).

    fread(file, 96, 1, infile);

output: 58 c8 c3 f7 41 22 b1 72 9f 41 50 37 cb 09 d0 11 1d 5a 48 59 96 11 fa 4b fd d5 86 70 18 2c 50 2f fd 67 71 3d 20 fc 17 e1 27 f7 9c be 03 74 74 56 6c 49 e9 ee 24 9d 0a 06 da 6a 80 20 4d 91 e9

How should I read all the data from the file?

3

There are 3 best solutions below

2
On BEST ANSWER

Maybe you have opened the file in text mode. Try opening it in binary mode.

1
On

If you are checking the data you read via printf, supplying a %s argument, then the 0 byte will terminate the output. Try printing with %x and in a loop.

0
On
#include <stdio.h>
void main(){
    freopen("inp.txt", "rb", stdin);
    int var;
    while(scanf("%x", &var) == 1){
        printf("%x ",var);
    }
}

Output

enter image description here