c - Reading binary file to a string but the types are mixed -
i'm trying read binary file when read characters doesn't looks it's formatted char
type example numbers don't have ascii value instead, actual value letters have ascii value.
why that?
also, when create binary file, doesn't hold '\0'
padding, nor \x
behind every number, , why show up?
this how i'm reading file:
file * fp = fopen("file.bin", "rb"); char foo[20]; fread(foo, sizeof(char), 20, fp);
which can see in vs, fills foo
this:
[0]: 5 '\x5' [1]: 0 '\0' [2]: 0 '\0' [3]: 0 '\0' [4]: 97 'a' [5]: 66 'b' [6]: 67 'c' [7]: 100 'd' [8]: 101 'e' [9]: 6 '\x6' [10]: 0 '\0' [11]: 0 '\0' [12]: 0 '\0' [13]: 97 'a' [14]: 97 'a' [15]: 66 'b' [16]: 84 't' [17]: 82 'r' [18]: 121 'y' [19]: 4 '\x4'
is there way read characters such hold ascii values? there way not read \0
, \x
?
as @chux said, \x
vs displaying inseparable part of char
representations vs presenting you. trying helpful providing form can used directly in c source code char
literal. example,
char c = '\x4';
it (separately) giving numeric value of each char
(expressed in decimal form).
how come numbers don't have ascii values though?
you said yourself: reading binary file. typically means numbers represented in binary form, not formatted form.
and what's \0 padding?
in data presented, goes numbers being in binary form. zero-value bytes appear not padding, rather parts of 4-byte numeric (little-endian) representations of numbers. thus, number 6
represented 4 bytes, having values 6
, 0
, 0
, 0
.
depending on how data written, however, there could padding between members. dealing issues such 1 of joys of working binary data formats. read data correctly need precise definition of form.
Comments
Post a Comment