g*********s 发帖数: 1782 | 1 For example:
big endian machine,fwrite(&x, sizeof(int), 1, output).
small endian machine, fread(&y, sizeof(int), 1, input).
Here input and output point to the same file.
Will fread/write transparently handle this issue? | t****t 发帖数: 6806 | 2 of course not. you need to handle it.
【在 g*********s 的大作中提到】 : For example: : big endian machine,fwrite(&x, sizeof(int), 1, output). : small endian machine, fread(&y, sizeof(int), 1, input). : Here input and output point to the same file. : Will fread/write transparently handle this issue?
| b******n 发帖数: 592 | 3 no, it won't. you need to handle it correctly
【在 g*********s 的大作中提到】 : For example: : big endian machine,fwrite(&x, sizeof(int), 1, output). : small endian machine, fread(&y, sizeof(int), 1, input). : Here input and output point to the same file. : Will fread/write transparently handle this issue?
| i*****f 发帖数: 578 | 4 GNU libc has an socket API handles this because tcp packet and x86 machine
uses different endians. I remember it is ntol() or lton().
【在 g*********s 的大作中提到】 : For example: : big endian machine,fwrite(&x, sizeof(int), 1, output). : small endian machine, fread(&y, sizeof(int), 1, input). : Here input and output point to the same file. : Will fread/write transparently handle this issue?
| l***i 发帖数: 1309 | 5 Here is one way to handle it.
#include
#include
#include
size_t write_uint32_to_file( FILE* file, uint32_t nbr )
{
nbr = htonl( nbr ); // Host to network long
return fwrite( file, &nbr, 4, 1 );
}
size_t read_uint32_from_file( FILE* file, uint32_t* nbr )
{
size_t res = fread( file, nbr, 4, 1 );
*nbr = ntohl( *nbr ); // Network to host long
return res;
}
【在 g*********s 的大作中提到】 : For example: : big endian machine,fwrite(&x, sizeof(int), 1, output). : small endian machine, fread(&y, sizeof(int), 1, input). : Here input and output point to the same file. : Will fread/write transparently handle this issue?
|
|