从“完整”中填写空二进制文件。一 [英] Telling an empty binary file from a "full" one

查看:66
本文介绍了从“完整”中填写空二进制文件。一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制文件,用于存储变量的值,以便
再次使用它们。我很容易知道文件是否存在,但

问题是,如果程序早先在它之前被中断,

可以将变量写入文件,文件将是空的,并且

然后它会加载一堆垃圾进入变量,我想要
避免。

该文件总是36字节大(它包含4个双精度浮点数

和一个整数),我希望能够测试它是否是36字节

多长时间,但以便携式方式进行购物似乎是个大问题。


我以为使用fseek和ftell可以工作,如果文件结尾

可以被告知,但我读到将文件位置指示器设置为

end-of-file,as as with fseek(file,0,SEEK_END),有未定义的行为

表示二进制流(因为可能是尾随空字符)"


我的文件有其中有很多零字节,所以我想这意味着它无法告诉

然后可靠地结束文件,对吧?我只想知道我怎样才能以可靠和便携的方式告诉我的二进制文件的大小,如果没有,请告诉我是否b $ b文件是否为空

解决方案

文章< 11 *************** *******@o13g2000cwo.googlegroups .com> ;,

Michel Rouzic< Mi ******** @ yahoo.fr>写道:

我有一个二进制文件,用于存储变量的值,以便再次使用它们。我很容易知道该文件是否存在,但问题是,如果程序早先在它可以将变量写入文件之前被中断,则该文件将为空,并且<然后它会加载一堆垃圾变成我想要避免的变量。


在这种情况下,你的装载程序是在没有适当考虑的情况下编程的。

条件。


每个fread您执行的()或fgetc()或fscanf()返回

状态。如果文件可能不是大小合适的话,你应该测试那些返回状态,

并且如果你没有得到适当的步骤足够的数据。

该文件总是36字节大(它包含4个双精度浮点数
和一个整数)




可能是最简单的便携式判断文件是否正确

大小将尝试fread)37个字节,并查看是否

递给更少的字节(文件截断的),36个字节(正确的大小),或37个字节

(文件太长了。)


我会说明你的意思已经强调了可移植性

的测试,但双精度浮点数的大小并不确定

为8个字节,整数不一定是4个字节。 br />
看来您可能没有留下任何标志的空间

来表示表示格式并指出哪个endian ;

数据输入。从二进制文件中可以轻松地将两个数字加在一起

数字并不好玩 - 定点或可打印文本或XDR更容易

来处理这方面的问题。

-

如果你喜欢VT-52',那就太费了。

Michel Rouzic< Mi ******** @ yahoo.fr>写道:

该文件总是36字节大(它包含4个双精度浮点数
和一个整数),我想能够测试它是否是36个字节长还是不长,但是以便携式方式做这件事似乎是一个很大的问题。




如果你假设它总是36字节大小,你已经离开了严格的可移植性领域。有什么特别的原因

你不能简单地存储听起来像少量数据的文件吗?无论如何,它会让事情变得更容易。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


文章< 11 ********************** @ o13g2000cwo.googlegroups。 com>,

Michel Rouzic< Mi ******** @ yahoo.fr>写道:


我的文件中有很多零字节,所以我猜这意味着它无法告诉
然后可靠地结束文件,对吗?我想知道我怎么能以可靠和可移植的方式告诉我的二进制文件的大小,如果没有,
告诉我的文件是否为空



我很好奇现有的操作系​​统没有准确地报告二进制文件的长度。有没有人

有任何例子?


我遇到的唯一这样的情况是

使用unix的系统类似的设备映射通常可以强制打开原始磁盘分区。由于

没有文件系统存在,fseek(SEEK_END)将

经常位于分区的末尾是否

你写的有意义数据与否。


I have a binary file used to store the values of variables in order to
use them again. I easily know whether the file exists or not, but the
problem is, in case the program has been earlier interupted before it
could write the variables to the file, the file is gonna be empty, and
then it''s gonna load a load of crap into variables, which i want to
avoid.

That file is always 36 bytes big (it contains 4 double-precision floats
and one integer) and i''d like to be able to test whether it is 36 bytes
long or not, but it seems like quite a big problem to get to do it in a
portable way.

I thought that using fseek and ftell could work if the end of file
could be told but i read that "Setting the file position indicator to
end-of-file, as with fseek(file, 0, SEEK_END), has undefined behavior
for a binary stream (because of possible trailing null characters)"

My file has lots of zero bytes in it, so I guess it means it can''t tell
then end of file reliably, right? I''d just like to know how I can, in a
reliable and portable way, tell the size of my binary file, and if not,
tell whether my file is empty or not

解决方案

In article <11**********************@o13g2000cwo.googlegroups .com>,
Michel Rouzic <Mi********@yahoo.fr> wrote:

I have a binary file used to store the values of variables in order to
use them again. I easily know whether the file exists or not, but the
problem is, in case the program has been earlier interupted before it
could write the variables to the file, the file is gonna be empty, and
then it''s gonna load a load of crap into variables, which i want to
avoid.
In that case, your load routine is programmed without due regard
to the circumstances.

Each fread() or fgetc() or fscanf() that you perform returns a
status. If there is any serious chance that the file might not
be of the proper size, you should be testing those return statuses,
and taking appropriate steps if you do not get enough data.
That file is always 36 bytes big (it contains 4 double-precision floats
and one integer)



Probably the easiest portable well to tell if the file is the right
size would be to attempt to fread) 37 bytes, and see whether you were
handed fewer bytes (file truncated), 36 bytes (right size), or 37 bytes
(file is too long.)

I would, though, make the point that you have emphasized portability
for the test, but the size of double-precision floats is not certain
to be 8 bytes, and integers are not certain to be 4 bytes.
It also appears that you might not have left room for any flags
to indicate representation format and to indicate which "endian"
the data is in. Portably stiching together a double from a binary
number is no fun -- fixed point or printable text or XDR are easier
to deal with in that regard.
--
Feep if you love VT-52''s.


Michel Rouzic <Mi********@yahoo.fr> wrote:

That file is always 36 bytes big (it contains 4 double-precision floats
and one integer) and i''d like to be able to test whether it is 36 bytes
long or not, but it seems like quite a big problem to get to do it in a
portable way.



If you''re assuming it will always be 36 bytes in size, you''ve already
left the realms of strict portability. Is there any particular reason
you''re unable to simply store what sounds like a small amount of data
as text? It would make many things easier, in any case.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


In article <11**********************@o13g2000cwo.googlegroups .com>,
Michel Rouzic <Mi********@yahoo.fr> wrote:


My file has lots of zero bytes in it, so I guess it means it can''t tell
then end of file reliably, right? I''d just like to know how I can, in a
reliable and portable way, tell the size of my binary file, and if not,
tell whether my file is empty or not



I''m curious as to what existing OS''s do not accurately
report the lengths of binary files. Does anyone
have any examples?

The only situation like this that I''ve encountered is
that systems with unix-like device mapping can often
be coerced into opening a raw disk partition. Since
there is no file system present, fseek(SEEK_END) will
often position to the end of the partition whether
you have written meaningful data or not.


这篇关于从“完整”中填写空二进制文件。一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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