• 0

[C] fprintf writing ascii instead of binary


Question

SOLVED see my next post in this thread

I feel so dumb for not being able to figure this out. Does it have anything to do with the double quotations?

FILE *file;
file = fopen("test", "wb");
fprintf(file, "%d", 0x000000FF);
fclose(file);

then I call hexdump -C test

and I see

00000000  32 35 35

instead of

00000000  FF 00 00 00

Edited by Richard Davison

3 answers to this question

Recommended Posts

  • 0

I think I overlooked the fwrite() function.

@Lant, if I just used %c, then yeah, FF would show up, but the remaining zeros would get lost. I would be writing 1 byte rather than the 4 bytes I want to write with an int.

@Pabs(Sco), with %x the hexdump gives me 66 66 which the ascii equivalent to FF

edit:

I think I've just been using the wrong function. I should probably be using fwrite instead of fprintf

edit again:

this code works

#include <stdio.h>

void main(void)
{
	int i = 0x000000FF;
	FILE *file;
	file = fopen("test", "wb");
	fwrite(&i, sizeof(int), 1, file);
	fclose(file);
}

it spits out ff 00 00 00

good night... *yawns*

Edited by Richard Davison
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.