将二进制数字写入文件 [英] Writing Binary Digits to a file

查看:75
本文介绍了将二进制数字写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个57000位长的二进制数,我想将它保存到文件中。


目前我使用unsigned char数组来存储数组和

然后fwrite存储它如下。


fwrite(BinArr,sizeof(unsigned char), 57000,BinFile);


但这是浪费空间,因为这需要大约57 KB,如果我可以存储,因为比特只会占用大约7 KB。


你能否告诉我是否可以这样做。


非常感谢。

速度。

解决方案

速度写道:


我有一个57000位长的二进制数,我想将它保存到文件中。


目前我正在使用unsigned char数组来存储数组



嗯?




然后fwrite将其存储如下。


fwrite(BinArr,sizeof(unsigned char),57000,BinFile);



为什么要写57000字节?难道你不是在写57000 / CHAR_BIT吗?


但这是浪费空间,因为这需要大约57 KB,如果我

可以存储,因为位只需要大约7 KB。



是的。你不是已经将你的''BinArr''的每一位映射到你的号码中的二进制数字

了吗?如果没有,你应该。


你能否告诉我是否可以这样做。



不确定你的意思。如果你说你有一个57000位长的二进制

数字,你怎么代表它?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


" Speed" < lo ********** @ gmail.com写信息

新闻:11 ******************** **@i3g2000cwc.googlegro ups.com ...





我有一个57000位长的二进制数我想将它保存到文件中。


目前我使用unsigned char数组存储数组和

然后fwrite存储它如下。


fwrite(BinArr,sizeof(unsigned char),57000,BinFile);


但它浪费空间这需要大约57 KB,如果我可以存储,因为位只需要大约7 KB。


你能否告诉我这是否可以完成。


非常感谢。

速度。



当然,只需存储使用它们的字节。你说它是'二进制

数字'。它是如何存储在变量中的? BCD(二进制编码十进制)?一些

其他代表?将它们存储在

程序中需要多少内存?

你说你有一个57000 * bit *长二进制数*。它是57,000位还是

位数?如果它是57000位,那么只需将它们存储为二进制。


unsigned BinArr char [57000 / sizeof(unsigned char)]; //这将是

57,000 *位*

//这个数组是在某处加载/使用的,或者你是如何使用这些57,000

比特?


fwrite(BinArr,sizeof(unsigned char),57000 / sizeof(unsigned char),

BinFile);


57000/8(我系统中unsigned char的sizeof)是7125,所以这些57000位

存储在7125字节中。


< blockquote> Jim Langston写道:


" Speed" < lo ********** @ gmail.comwrote in message


>我有一个57000位长的二进制数,我想保存它到一个文件。



[...]


你说你有一个57000 * bit *长二进制数*。它是57,000位还是

位数?如果它是57000位,那么只需将它们存储为二进制。


unsigned BinArr char [57000 / sizeof(unsigned char)]; //这将是

57,000 *位*

//这个数组是在某处加载/使用的,或者你是如何使用这些57,000

比特?


fwrite(BinArr,sizeof(unsigned char),57000 / sizeof(unsigned char),

BinFile);


57000/8(我的系统中unsigned char的sizeof)是7125,所以这些57000位

存储在7125字节中。



sizeof(char)和sizeof(unsigned char)是 - 按照定义 - 1.

所以57000 / sizeof(unsigned char)是57000.你想要CHAR_BITS。


-

Thomas
http://www.netmeister.org/news/learn2quote.html


Hi,

I have a 57000 bit long binary number and I want to save it to a file.

At the moment I am using an unsigned char array to store the array and
then fwrite to store it as follows.

fwrite( BinArr, sizeof(unsigned char), 57000, BinFile);

But it is a waste of space as this takes up around 57 KB which if I
could store as bits would only take up around 7 KB.

Could you please tell me if this could be done.

Thanks a ton.
Speed.

解决方案

Speed wrote:

I have a 57000 bit long binary number and I want to save it to a file.

At the moment I am using an unsigned char array to store the array

Huh?

and
then fwrite to store it as follows.

fwrite( BinArr, sizeof(unsigned char), 57000, BinFile);

Why are you writing 57000 bytes? Shouldn''t you be writing 57000/CHAR_BIT?

But it is a waste of space as this takes up around 57 KB which if I
could store as bits would only take up around 7 KB.

Yes. Don''t you already map each bit of your ''BinArr'' to the binary digit
in your number? If not, you should.

Could you please tell me if this could be done.

Not sure what you mean. When you say you have "a 57000 bit long binary
number", how do you represent it?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


"Speed" <lo**********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

Hi,

I have a 57000 bit long binary number and I want to save it to a file.

At the moment I am using an unsigned char array to store the array and
then fwrite to store it as follows.

fwrite( BinArr, sizeof(unsigned char), 57000, BinFile);

But it is a waste of space as this takes up around 57 KB which if I
could store as bits would only take up around 7 KB.

Could you please tell me if this could be done.

Thanks a ton.
Speed.

Sure, just store the bytes as you''re using them. You say it''s a "binary
number". How is it stored in variables? BCD (Binary Coded Decimal)? Some
other representation? How much memory does it take to store them in the
program?
You say you have a "57000 *bit* long binary number*. Is it 57,000 bits or
digits? If it''s 57000 bits, then just store them as binary.

unsigned BinArr char[57000 / sizeof( unsigned char )]; // this would be
57,000 *bits*
// This array is loaded/used somewhere, or how are you using these 57,000
bits?

fwrite( BinArr, sizeof( unsigned char), 57000 / sizeof( unsigned char),
BinFile );

57000 / 8 (sizeof unsigned char on my system) is 7125, so these 57000 bits
are stored in 7125 bytes.


Jim Langston wrote:

"Speed" <lo**********@gmail.comwrote in message

>I have a 57000 bit long binary number and I want to save it to a file.

[...]

You say you have a "57000 *bit* long binary number*. Is it 57,000 bits or
digits? If it''s 57000 bits, then just store them as binary.

unsigned BinArr char[57000 / sizeof( unsigned char )]; // this would be
57,000 *bits*
// This array is loaded/used somewhere, or how are you using these 57,000
bits?

fwrite( BinArr, sizeof( unsigned char), 57000 / sizeof( unsigned char),
BinFile );

57000 / 8 (sizeof unsigned char on my system) is 7125, so these 57000 bits
are stored in 7125 bytes.

sizeof(char) and sizeof(unsigned char) is - by definition - 1.
So 57000 / sizeof(unsigned char) is 57000. You want CHAR_BITS.

--
Thomas
http://www.netmeister.org/news/learn2quote.html


这篇关于将二进制数字写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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