fread / fwrite可移植性问题 [英] fread/fwrite Portability Issues

查看:113
本文介绍了fread / fwrite可移植性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-----开始PGP签名消息-----

哈希:SHA1


嘿所有。

我正试图找到一种方法(便携)将32位整数值写入

文件。就目前而言,我一直在使用这样的东西:


#include< stdio.h>


/ *写入val到f指向的文件。 * /

int write32(unsigned long val,FILE * f)

{

unsigned char c;

int i;


/ *确保指针有效。 * /

if(!f)

{

fprintf(stderr,NULL file pointer.\ n);

返回1;

}


/ *将4个字节从LSB写入MSB。 * /

for(i = 0; i< 4; i ++)

{

c =(val>(i * 8) )& 0xff;

if(fwrite(& c,1,1,f)!= 1)

{

/ *确保写作发生了。 * /

fprintf(stderr,Write error.\ n);

返回1;

}

}


/ *正常退出。 * /

返回0;

}


这似乎有用,但我被告知char并不总是8位。这个

意味着在具有较大字符大小的big-endian系统上,我将写入

全部为零。


有人可以推荐替代品吗?


- -

问候,

Jonathan Lamothe


/ *

*糟糕。内核试图访问一些糟糕的页面。我们将不得不以极端的偏见终止事情。

* /


die_if_kernel(" Oops", regs,error_code);

- 来自linux / arch / i386 / mm / fault.c

----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.4.2(GNU / Linux)

评论:将GnuPG与Thunderbird一起使用 - http://enigmail.mozdev.org

iD8DBQFEwm7wq9nD47x87JYRAoMGAJ957wTuvop8ijiHMOrvaT 81c6b6 + wCfQulN

DiVY3WI5ORK + oK2YJgF0sas =

= RAEB

----- END PGP SIGNATURE -----

-----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

iD8DBQFEwm7wq9nD47x87JYRAoMGAJ957wTuvop8ijiHMOrvaT 81c6b6+wCfQulN
DiVY3WI5ORK+oK2YJgF0sas=
=RAEB
-----END PGP SIGNATURE-----

推荐答案



Jonathan Lamothe写道:

Jonathan Lamothe wrote:

-----开始PGP签名消息-----

哈希:SHA1


嘿所有。


我正试图找到一种方法(便携)写32位整数值到一个

文件。就目前而言,我一直在使用这样的东西:
-----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:



< snip>

<snip>


c =(val>(i * 8))& 0xff;

if(fwrite(& c,1,1,f)!= 1)
c = (val >(i * 8)) & 0xff;
if(fwrite(&c, 1, 1, f) != 1)



否,转换为缓冲区然后调用fwrite一次。你正在调用单个字节的

函数,这非常低效。

No, convert to a buffer then call fwrite once. You''re calling the
function for single bytes and that''s terribly inefficient.


这似乎有用,但我'告诉我,char并不总是8位。这个

意味着在具有较大字符大小的big-endian系统上,我将写入

全部为该文件的零。
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.



嗯,没有。如果您将数据存储在较低的32位中,那么总是可以使用
工作。也就是说,无论是否有结尾


无符号x = 0x1234;

x>> = 8;


x现在将等于0x12,无论平台是大,小甚至是
中端。


Tom

Um, no. If you store data in the lower 32-bits then that will always
work. That is, regardless of endianess

unsigned x = 0x1234;
x >>= 8;

x will now equal 0x12 regardless if the platform is big, little or even
middle endian.

Tom


Tom St Denis写道:
Tom St Denis wrote:

Jonathan Lamothe写道:
Jonathan Lamothe wrote:

- ---开始PGP签名消息-----

哈希:SHA1


嘿所有。


我正试图找到一种方法(便携地)将32位整数值写入

文件。就目前而言,我一直在使用这样的东西:
-----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:



< snip>


<snip>


c =(val>(i * 8))& 0xff;

if(fwrite(& c,1,1,f)!= 1)
c = (val >(i * 8)) & 0xff;
if(fwrite(&c, 1, 1, f) != 1)



否,转换为缓冲区然后调用fwrite一次。你在单个字节上调用

函数,这非常低效。


No, convert to a buffer then call fwrite once. You''re calling the
function for single bytes and that''s terribly inefficient.



为什么效率低下?无论如何,fwrite可能会将字节放在

a缓冲区中。

Why would it be inefficient ? fwrite will probably put the bytes in
a buffer anyway.


这似乎有用,但我'告诉我,char并不总是8位。这个

意味着在具有较大字符大小的big-endian系统上,我将写入

全部为该文件的零。
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.



嗯,没有。如果您将数据存储在较低的32位中,那么总是可以使用
工作。也就是说,无论endianess


Um, no. If you store data in the lower 32-bits then that will always
work. That is, regardless of endianess



确实,该程序是正确的。我无法想象为什么Jonathan认为

如果char大于8位,零将被存储到文件中。


Spiros Bousbouras


spi ... @ gmail.com写道:
spi...@gmail.com wrote:

确实,程序是正确的。我无法想象为什么乔纳森认为如果char大于8位,零将被存储到文件中。
Indeed , the programme is correct. I can''t imagine why Jonathan thinks
that zeros will be stored to the file if char is greater than 8 bits.



他可能在64位平台上认为


无符号x = 0x1234;


意味着


x = 0x1234000000000000


和8的移位得到LSB零。


当然,[对于OP]在内存中它可以存储为


12 34 00 00 00 00 ...


但是当你将它用作数据类型时,操作已经明确定义了

行为。所以x& 255"在你修改x之前总是为0x34。


你是对的,你不能只记忆或写下未签名的[或

无论什么]类型到一个文件,并期望代码在其他地方工作。

但是,如果你[正确]掩盖了不变的字节,它将作为

所需。


Tom

He''s probably thinking that on a 64-bit platform that

unsigned x = 0x1234;

would mean

x = 0x1234000000000000

And the shift by 8 gets the LSB zeroes.

Sure, [for the OP] in memory it may be stored as

12 34 00 00 00 00 ...

But when you use it as a data type the operations have well defined
behaviours. So "x & 255" is always 0x34 until you modify x.

You''re correct that you can''t just memcpy or fwrite the unsigned [or
whatever] types to a file and expect the code to work elsewhere.
However, if you [correctly] mask off the invidual bytes it will work as
desired.

Tom


这篇关于fread / fwrite可移植性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆