mathjacks
mathjacks

Reputation: 335

How do I use fwrite to write the data 0xf0f0f0f0

I need to use fwrite to write a delimiter = 0xf0f0f0f0 to a binary file. It needs to be done in such a way that 0xf0f0f0f0 appears at the beginning of the file when viewed in xxd on UNIX. I'm currently doing it this way,

  unsigned int delim[1];
  delim[1] = 0xf0f0f0f0;
  fwrite(delim, 4, 1, destination);

But it's clearly not working since xxd is showing this at the beginning of the file:

  0000000: 90c9 49ac

Upvotes: 2

Views: 542

Answers (2)

Alex Reynolds
Alex Reynolds

Reputation: 96937

This:

unsigned int delim[1];

is an array containing one element.

This:

delim[1] = 0xf0f0f0f0;

refers to the second element in a single-element array, which is not correct code.

Change delim[1] to delim[0], if you want to put something in your single-element array.

Upvotes: 5

Probably should be

 unsigned int delimnum = 0xf0f0f0f0;
 fwrite(&delimnum, sizeof(delimnum), 1, destination);

Or at least use delim[0] since array indexing starts at 0.

Upvotes: 10

Related Questions