Mossmyr Mossmyr - 1 year ago 90
C Question

Writing number as raw binary to file

So I'm trying to write the value of a

as raw binary to a file with
, here's a test file:

int main() {
FILE* stream;
stream = fopen("write", "wb");
long number = 0xfff;
fwrite(&number, sizeof(long), 1, stream);

When opening the written file in a text editor, this is the output:

ff0f 0000 0000 0000

while I was expecting something like
0000 0000 0000 0fff

How do I write the desired result correctly as raw binary?

Answer Source

There's no problem. You're seeing it as ff0f 0000 0000 0000 cause of the endian of the machine! Try using fread() instead!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download