为什么这段代码可以告诉二进制文件的EOF? [英] why can this code tell the EOF of binary file?

查看:107
本文介绍了为什么这段代码可以告诉二进制文件的EOF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道文本文件以EOF标记结束但是

二进制文件没有标记。

所以,问题是我们怎么知道二进制文件的结尾是什么?


这段代码可以告诉我们什么时候到达文件结束


int ch;


FILE * fp;


fp = fopen(" filename"," rb");


if(fp!= NULL){

while((ch = getc(fp))!= EOF){

/ *你的代码* /

}


}


我的问题是,因为二进制没有结束标记文件,为什么

上面的代码是否正常工作?

,为什么内置函数feof(fp)知道文件的末尾在哪里?

感谢您的所有讨论。

聊天watchara

Hi,
I know that text file ended with EOF mark but there is no mark for
binary file.
So, the problem is how do we know the end of binary file is reach?

This code can tell us when the end of file is reach

int ch;

FILE *fp;

fp = fopen("filename","rb");

if(fp != NULL) {
while((ch = getc(fp)) != EOF) {
/* your code */
}

}

My question is , since there is no ending mark up for binary file, why
does the code above work?
also, why does built-in function feof(fp) know where is end-of-file?
thank you for all discussions.
chat watchara

推荐答案

chat写道:
chat wrote:

我知道文本文件以EOF标记结束但是没有标记为

二进制f ILE。
I know that text file ended with EOF mark but there is no mark for
binary file.



这不是真的。

C标准没有定义文本/文件/结尾。什么/是/

定义当你尝试使用(f)getc读取FILE * [连接到文件]的倒数第二个
字符时会发生什么:你得到

魔法值EOF。无论您是否正在阅读

a文本文件,这都是正确的。


[但是,此EOF可能会先于某个数字0 's - ISTR

它是零 - 在逻辑上不存在于文件中,因为有些

系统在某个时间确实或仍然没有记录确切的数字
保存在文件中的
字节:文件可能包含

/块/字节的整数。]

This is not true.

How a text /file/ ends isn''t defined by the C standard. What /is/
defined is what happens when you try and read the postultimate
character of a FILE* [connected to a file] using (f)getc: you get
the magic value EOF. This is true whether or not you''re reading
a "text file".

[However, this EOF may be preceeded by some number of 0''s -- ISTR
it''s zeroes -- not logically present in the file, because some
systems at some time did or still do not record the exact number
of bytes kept in a file: files may contain integral numbers of
/blocks/ of bytes.]


所以,问题是我们如何知道二进制文件的结尾?
So, the problem is how do we know the end of binary file is reach?



等待EOF。

Wait for EOF.


另外,为什么内置函数feof(fp)知道哪里是文件结尾?
also, why does built-in function feof(fp) know where is end-of-file?



因为它的业务是[最后一次读取失败,因为

它位于文件末尾]。请注意,`feof`通常是错误的功能

要打电话。


-

Chris特别是对于例如傅里叶变换 Dollin

我们没有时间找到我们想知道的一切。

- James Blish,/一个Cy钹的冲突/

Because it''s its business to know [that the last read failed because
it was at end-of-file]. Note that `feof` is often the wrong function
to be calling.

--
Chris "especially for eg Fourier transforms" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/


聊天写道:
chat writes:

我知道文本文件以EOF标记结束但是没有标记为

二进制文件。

所以,问题是我们怎么知道二进制文件的结尾是什么?
I know that text file ended with EOF mark but there is no mark for
binary file.
So, the problem is how do we know the end of binary file is reach?



文件系统中的文件通常具有相关的文件大小,该文件大小为文件系统中文件的物理大小。这可能是

字节的大小,在这种情况下没有EOF标记。需要标记文本结尾

文件。但是如果大小是块,那么二进制文件的大小为

,这是块大小的倍数。 (对于关联文件大小而言,文本文件也是如此;但是EOF标记可以在物理文件结束之前将其分隔为b $ b。)

A file in a file system normally has an associated file size which gives
the file''s physical size in the file system. That may be a size in
bytes, in which case no "EOF mark" is needed to mark the end of text
files. But if the size is in blocks, then a binary file has a size
which is a multiple of the block size. (So does a text file as far as
the associatead file size is concerned, but an "EOF mark" can delimit it
before the end of physical file.)


(...)

fp = fopen(" filename"," rb");

if(fp!= NULL){

while((ch = getc(fp))!= EOF){

/ *您的代码* /

}


}


我的问题是,由于二进制文件没有结束标记,为什么

上面的代码是否正常工作?
(...)
fp = fopen("filename","rb");
if(fp != NULL) {
while((ch = getc(fp)) != EOF) {
/* your code */
}

}

My question is , since there is no ending mark up for binary file, why
does the code above work?



当它到达文件的物理末尾时结束。

It ends when it reaches the physical end of the file.


,为什么建立 - 在函数feof(fp)知道文件的末尾在哪里?
also, why does built-in function feof(fp) know where is end-of-file?



feof()在getc()返回EOF时返回true,而不是之前。


-

Hallvard

feof() returns true when getc() has returned EOF, not before.

--
Hallvard


" chat" < ch *********** @ gmail.comwrote:
"chat" <ch***********@gmail.comwrote:

我知道文本文件以EOF标记结束但是没有标记

二进制文件。

所以,问题是我们怎么知道二进制文件的结尾是什么?
I know that text file ended with EOF mark but there is no mark for
binary file.
So, the problem is how do we know the end of binary file is reach?



我们没有必要;实现必须告诉我们(通过返回EOF

而不是值字节,例如),它通常从底层操作系统获取该信息


We don''t have to; the implementation has to tell us (by returning EOF
instead of a value byte, e.g.), and it typically gets that information
from the underlying operating system.


我的问题是,因为二进制文件没有结束标记,
My question is , since there is no ending mark up for binary file,



嗯,有。它只是不在二进制文件数据本身内。

通常它是目录结构中的长度字段,或磁盘分配表中的块数

或类似外部的东西。

Well, there is. It just isn''t inside the binary file data itself.
Usually it''s a length field in the directory structure, or a block count
in the disk allocation tables, or something external like that.


,为什么内置函数feof(fp)知道文件结尾在哪里?
also, why does built-in function feof(fp) know where is end-of-file?



它本身并不知道。所有feof()都表示最后一个

函数用于从流信号中读取文件末尾;那些,再次,
,从操作系统获取信息,从

目录结构或类似的东西获取它。


Richard

It doesn''t know that itself. All feof() does is signal that the last
function used to read from a stream signalled end of file; and those,
again, get that information from the OS, which gets it from the
directory structure or something similar.

Richard


这篇关于为什么这段代码可以告诉二进制文件的EOF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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