vendredi 29 mai 2015

Reading binary file to a string but the types are mixed

I'm trying to read a binary file but when I read its characters it doesn't looks like it's formatted with char type so for example the numbers don't have their ASCII value instead, their actual value but letters does have ASCII value.

Why is that?

Also, when I create a binary file, it doesn't hold all those '\0' padding, nor the \x behind every number, what are those and why do they show up?

This is how I'm reading the file:

FILE * fp = fopen("file.bin", "rb");
char foo[20];
fread(foo, sizeof(char), 20, fp);

Which I can see in VS, fills foo with 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 a way to read all characters such that all will hold ASCII values? Is there a way to not read the \0 and \x?

Aucun commentaire:

Enregistrer un commentaire