J
Jonathan Lamothe
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hey all.
I'm trying to find a way to (portably) write 32-bit integer values to a
file. As it stands, I've been using something like this:
#include <stdio.h>
/* Write val to the file pointed to by f. */
int write32(unsigned long val, FILE *f)
{
unsigned char c;
int i;
/* Make sure the pointer is valid. */
if(!f)
{
fprintf(stderr, "NULL file pointer.\n");
return 1;
}
/* Write the 4 bytes from LSB to MSB. */
for(i = 0; i < 4; i++)
{
c = (val >> (i * 8)) & 0xff;
if(fwrite(&c, 1, 1, f) != 1)
{
/* Ensure the writing occurred. */
fprintf(stderr, "Write error.\n");
return 1;
}
}
/* Exit normally. */
return 0;
}
This seems to work, but I'm told that char is not always 8 bits. This
means that on big-endian systems with larger char sizes, I'll be writing
all zeros to the file.
Can anyone suggest an alternative?
- --
Regards,
Jonathan Lamothe
/*
* Oops. The kernel tried to access some bad page. We'll have to
* terminate things with extreme prejudice.
*/
die_if_kernel("Oops", regs, error_code);
-- From linux/arch/i386/mm/fault.c
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFEwm7wq9nD47x87JYRAoMGAJ957wTuvop8ijiHMOrvaT81c6b6+wCfQulN
DiVY3WI5ORK+oK2YJgF0sas=
=RAEB
-----END PGP SIGNATURE-----
Hash: SHA1
Hey all.
I'm trying to find a way to (portably) write 32-bit integer values to a
file. As it stands, I've been using something like this:
#include <stdio.h>
/* Write val to the file pointed to by f. */
int write32(unsigned long val, FILE *f)
{
unsigned char c;
int i;
/* Make sure the pointer is valid. */
if(!f)
{
fprintf(stderr, "NULL file pointer.\n");
return 1;
}
/* Write the 4 bytes from LSB to MSB. */
for(i = 0; i < 4; i++)
{
c = (val >> (i * 8)) & 0xff;
if(fwrite(&c, 1, 1, f) != 1)
{
/* Ensure the writing occurred. */
fprintf(stderr, "Write error.\n");
return 1;
}
}
/* Exit normally. */
return 0;
}
This seems to work, but I'm told that char is not always 8 bits. This
means that on big-endian systems with larger char sizes, I'll be writing
all zeros to the file.
Can anyone suggest an alternative?
- --
Regards,
Jonathan Lamothe
/*
* Oops. The kernel tried to access some bad page. We'll have to
* terminate things with extreme prejudice.
*/
die_if_kernel("Oops", regs, error_code);
-- From linux/arch/i386/mm/fault.c
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFEwm7wq9nD47x87JYRAoMGAJ957wTuvop8ijiHMOrvaT81c6b6+wCfQulN
DiVY3WI5ORK+oK2YJgF0sas=
=RAEB
-----END PGP SIGNATURE-----