四个或两个字节? [英] Four or Two Bytes?

查看:89
本文介绍了四个或两个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道以下答案吗?一个unsigned short是2

字节长。为什么以下文件创建为4个字节而不是

2?


file = fopen(" data.bin"," wb");

if(file!= NULL)

{

unsigned short s = 65535;

printf("无符号短的大小:%d bytes \ n",sizeof(unsigned

short)); // 2个字节 - 好的

fwrite(& s,sizeof(unsigned short),sizeof(s),file);

fclose(file); //文件写成4个字节...嗯

}


- Hahnemann

解决方案

Hahnemann< ha ************* @ gmail.comwrites:


>有人知道以下答案吗? unsigned short是2
字节长。为什么将以下文件创建为4个字节而不是
2?


> file = fopen(" data.bin"," wb");
if(file!= NULL)

unsigned short s = 65535;

printf(" unsigned short的大小:%d bytes \ n",sizeof(unsigned
短)); // 2个字节 - 好的

fwrite(& s,sizeof(unsigned short),sizeof(s),file);

fclose(file); //文件写成4个字节...嗯
}



因为你要求它?

检查fwrite()的第3个参数。


-

克里斯。


5月29日,9:39 * pm,Chris McDonald< ch ... @ csse.uwa.edu.auwrote:


Hahnemann< hahnemann.or ... @ gmail.com写的:


有人知道以下答案吗?一个unsigned short是2

字节长。 *为什么以下文件创建为4个字节而不是

2?

file = fopen(" data.bin"," wb");

if(file!= NULL)

{

* * unsigned short s = 65535;

* * printf(" ;无符号短的大小:%d bytes \ n",sizeof(unsigned

short)); // 2个字节 - 好的

* * fwrite(& s,sizeof(unsigned short),sizeof(s),file);

* * fclose(file) ; //文件写成4个字节...嗯

}



因为你要求它?

检查fwrite()的第3个参数。


-

克里斯。



但请注意:


printf(" s的大小:%d bytes \ n",sizeof( s));


返回2个字节。如何使用以下文件创建2个字节的文件:


unsigned short x = 65535;


这是否可能?


"哈内曼" < ha ************* @ gmail.comwrote in message

news:2f ***************** ***************** @ d1g2000h sg.googlegroups.com ...


有谁知道答案以下?一个unsigned short是2

字节长。为什么以下文件创建为4个字节而不是

2?


file = fopen(" data.bin"," wb");

if(file!= NULL)

{

unsigned short s = 65535;

printf("无符号短的大小:%d bytes \ n",sizeof(unsigned

short)); // 2个字节 - OK


fwrite(& s,sizeof(unsigned short),sizeof(s),file);



fwrite(

指向缓冲区的指示... ok

该缓冲区元素的大小.. .2

要写的元素数量... 2

文件写入句柄应该发生... ok


因此,每个2个字节的2个元素应写入4个字节。


fclose(file); //文件写为4个字节... hmm



所以,fwrite工作正常。或多或少,因为它从

a 2字节对象中写出了4个字节。它可能很容易就崩溃了超出对象范围的


}



- 比尔


Does anybody know the answer to the following? An unsigned short is 2
bytes long. Why is the following file created as 4 bytes instead of
2?

file = fopen("data.bin", "wb");
if (file != NULL)
{
unsigned short s = 65535;
printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK
fwrite(&s, sizeof(unsigned short), sizeof(s), file);
fclose(file); // File written as 4 bytes... hmm
}

- Hahnemann

解决方案

Hahnemann <ha*************@gmail.comwrites:

>Does anybody know the answer to the following? An unsigned short is 2
bytes long. Why is the following file created as 4 bytes instead of
2?

>file = fopen("data.bin", "wb");
if (file != NULL)
{
unsigned short s = 65535;
printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK
fwrite(&s, sizeof(unsigned short), sizeof(s), file);
fclose(file); // File written as 4 bytes... hmm
}


Because you''re asking it to?
Check the 3rd parameter of fwrite().

--
Chris.


On May 29, 9:39*pm, Chris McDonald <ch...@csse.uwa.edu.auwrote:

Hahnemann <hahnemann.or...@gmail.comwrites:

Does anybody know the answer to the following? An unsigned short is 2
bytes long. *Why is the following file created as 4 bytes instead of
2?
file = fopen("data.bin", "wb");
if (file != NULL)
{
* *unsigned short s = 65535;
* *printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK
* *fwrite(&s, sizeof(unsigned short), sizeof(s), file);
* *fclose(file); // File written as 4 bytes... hmm
}


Because you''re asking it to?
Check the 3rd parameter of fwrite().

--
Chris.

But notice that:

printf("Size of s: %d bytes\n", sizeof(s)) ;

returns 2 bytes. How can I make the file 2 bytes using:

unsigned short x = 65535;

Is this even possible?


"Hahnemann" <ha*************@gmail.comwrote in message
news:2f**********************************@d1g2000h sg.googlegroups.com...

Does anybody know the answer to the following? An unsigned short is 2
bytes long. Why is the following file created as 4 bytes instead of
2?

file = fopen("data.bin", "wb");
if (file != NULL)
{
unsigned short s = 65535;
printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK

fwrite(&s, sizeof(unsigned short), sizeof(s), file);

fwrite(
pointer to buffer ... ok
size of an element of that buffer ... 2
number of elements to write ... 2
handle for file write should occur to ... ok

So, 2 elements of 2 bytes each should write four bytes.

fclose(file); // File written as 4 bytes... hmm

So, fwrite worked correctly. More or less, since it wrote out 4 bytes from
a 2 byte object. It could just about as easily have crashed when it went
beyond the bounds of the object.

}

- Bill


这篇关于四个或两个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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