文件大小 [英] Size of file

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

问题描述

我听说这并不总是有效的:


FILE * in;

长尺寸;

in = fopen(" foo.bar"," rb");

fseek(in,0,SEEK_END);

size = ftell(in);

fseek(in,0,SEEK_SET);


然后将大量字节大小写入内存。


显然fseek是因为0xFF EOF或其他

字符而无法保证工作,这仅在文本模式或二进制模式下是真的吗?无论如何都有

来获取文件大小而不必一次读取字节数。它是否最好直到它才会失败?

I hear that this isn''t always valid:

FILE *in;
long size;
in = fopen("foo.bar","rb");
fseek(in,0,SEEK_END);
size = ftell(in);
fseek(in,0,SEEK_SET);

then fread size many bytes into memory.

Apparently fseek is not guaranteed to work because of 0xFF EOF or other
characters, is this true only in text mode or also in binary mode? Is there
anyway to get a filesize without having to read bytes on at a time. Is it
best to just fread until it fails?

推荐答案



" MisterE" ; < Mi ***** @ nimga.com在留言中写道

news:48 ********************** @ news。 optusnet.com.au ...

"MisterE" <Mi*****@nimga.comwrote in message
news:48**********************@news.optusnet.com.au ...

>我听说这并不总是有效的:


FILE * in;

长尺寸;

in = fopen(" foo.bar"," rb");

fseek(in ,0,SEEK_END);

size = ftell(in);

fseek(in,0,SEEK_SET);


然后fread将大量字节大小写入内存。


显然由于0xFF EOF或其他

字符,fseek无法保证工作,仅在文本模式下才是这样还是二进制模式?无论如何都要获得一个文件大小而无需一次读取字节数。

最好是直到它失败才会崩溃?
>I hear that this isn''t always valid:

FILE *in;
long size;
in = fopen("foo.bar","rb");
fseek(in,0,SEEK_END);
size = ftell(in);
fseek(in,0,SEEK_SET);

then fread size many bytes into memory.

Apparently fseek is not guaranteed to work because of 0xFF EOF or other
characters, is this true only in text mode or also in binary mode? Is
there anyway to get a filesize without having to read bytes on at a time.
Is it best to just fread until it fails?



适用于我,使用二进制模式文件。但是有各种各样的缺陷:


在文本模式下,你得到的大小可能是错的,因为它可能包含''\ n''

'' \'''序列而不仅仅是''\ n''。


某些类型的文件可能没有开头或结尾(如stdin或某些

串口设备),所以没有大小。


有些操作系统可能无法存储文件的确切字节大小(例如可能只有

存储块大小),因此值可能是近似值。 (并且可能还有其他操作系统需要考虑的其他操作系统内容,例如使用压缩。)


如果文件大小,你获得的文件大小可能会改变在您使用文件大小信息时修改(通过任何

其他进程)。


有关详细信息,请参阅clc中有关此主题的主题


但是在这些限制条件下,我已经成功地使用了像你这样的代码

十年或两年。


-

Bartc

Works for me, using binary mode files. But there are various pitfalls:

In text mode, the size you get might be wrong because it might include ''\n''
''\r'' sequences instead of just ''\n''.

Some types of files may not have a beginning or end (like stdin, or some
serial device), so don''t have a size.

Some OSs may not store the exact bytesize of a file (for example may only
store a block size), so the value might be approximate. (And there might be
other OS things to bear in mind such as use of compression.)

And whatever file size you get might change if the file is modified (by any
other process) by the time you use the file size information.

For more details, see threads on this subject in c.l.c.

But within those constraints, I''ve been using code like yours successfully
for a decade or two.

--
Bartc


>我听说这并不总是有效的:


文件大小有很多很多不同的定义,

(可能比64位机器上的文件大小更多)和

你需要决定你想要使用哪个定义如果你打算

调用任何结果正确"或者不正确。
>I hear that this isn''t always valid:

There are many, many, many different definitions of "file size",
(probably more than there are file sizes on a 64-bit machine) and
you need to decide which definition you want to use if you intend
calling any result "correct" or "incorrect".

> FILE * in;
长尺寸;
in = fopen(" foo.bar", " rb";
fseek(in,0,SEEK_END);
size = ftell(in);
fseek(in,0,SEEK_SET);

然后将大量字节大小写入内存。
>FILE *in;
long size;
in = fopen("foo.bar","rb");
fseek(in,0,SEEK_END);
size = ftell(in);
fseek(in,0,SEEK_SET);

then fread size many bytes into memory.



在二进制模式下,SEEK_END不需要得到有意义的支持,因为系统可以用尾随的0字节填充文件。例如,CP / M

只对二进制文件上的扇区进行计数,并将文件大小四舍五入到下一个128字节的倍数,并填充最后一个扇区

尾随0字节。


在文本模式下,从ftell返回的大小无需像

a数字那样有意义。例如,它可能是多个值的位域

,如扇区,头部,圆柱体,轨道,火车等等,因此减去其中两个不是b $ b b给出任何有意义的东西。


(例如,尝试从10022008减去09302008,将它们视为十进制整数而不是日期,并尝试理解

结果表明它们相隔2天。

可以对文本文件偏移进行相同类型的编码。)

字节偏移到文本文件可能会产生误导,因为某些系统(例如Windows)完成了\\\\ n的翻译。


In binary mode, SEEK_END need not be meaningfully supported because
the system may pad the file with trailing 0 bytes. For example, CP/M
only counts sectors on binary files and rounds the size of the file
up to the next multiple of 128 bytes, and pads the last sector with
trailing 0 bytes.

In text mode, the size returned from ftell need not be meaningful as
a number. For example, it might be a bitfield of a number of values
like sector, head, cylinder, track, train, etc. so that subtracting
two of them does not give anything meaningful.

(Try, for example, subtracting 09302008 from 10022008, treating
them as decimal integers rather than dates, and try to make sense
out of the result that would indicate that they are 2 days apart.
The same kind of encoding can be done on text file offsets.)

Byte offsets into a text file are likely to be misleading because
of the \r\n -\n translation done by some systems (e.g. Windows).


>由于0xFF EOF或其他
字符,显然fseek不能保证工作,
>Apparently fseek is not guaranteed to work because of 0xFF EOF or other
characters,



没有EOF字符。即使在那些使用

作为文本文件(Windows)结束标记的系统上,该标记也不是0xFF。

许多系统(UNIX和变体)只是存储文件长度(另一个

定义的文件大小)并且不要使用结束标记。


EOF是一个值不适合*在char中(除非sizeof int ==

sizeof char)这就是为什么getchar()返回int,而不是char。

There is no "EOF character". Even on one those systems which use
an end marker for text files (Windows), that marker isn''t 0xFF.
Many systems (UNIX & variants) just store a file length (yet another
definition of "file size") and don''t use an end marker.

EOF is a value that won''t *fit* in a char (unless sizeof int ==
sizeof char) which is why getchar() returns int, not char.


>仅在文本模式或二进制模式下才是真的吗?
>is this true only in text mode or also in binary mode?



你因为不同的原因而在文本模式和二进制模式下都被搞砸了。

You are screwed in both text mode and in binary mode for different
reasons.


>无论如何都要
获取文件大小
>Is there
anyway to get a filesize



你想要* A * filesize(在这种情况下,我选0,它很容易,而且

你没有说它必须是正确的,有些文件实际上是

的大小为0)或者你想要*更正*文件大小,在这种情况下你需要选择文件大小的定义吗?

Do you want *A* filesize (in which case, I pick 0, it''s easy, and
you didn''t say it had to be correct, and some files actually do
have size 0) or do you want a *correct* filesize, in which case you
have to pick a definition of filesize?


>而不必在a上读取字节数时间。它是否最好只是在它失败之前畏缩?
>without having to read bytes on at a time. Is it
best to just fread until it fails?



如果你想把文件读入内存,可以想到两个定义

的文件大小:

1.在二进制模式下从文件读取的字节数。

2.在文本模式下从文件读取的字节数。


很可能这两个定义会为任何给定文件的文件大小提供不同的答案。这些

都不一定说明文件占用多少空间

磁盘。但是如果你想把文件读入内存,那么这些是正确的定义(选择使用相同的

文件模式作为文件模式的那个) 将要使用。

If you want to read the file into memory, two definitions
of file size come to mind:

1. The number of bytes read from the file in binary mode.
2. The number of bytes read from the file in text mode.

Chances are high that these two definitions will give different
answers for the file size for any given file. Neither of these
necessarily says anything about how much space the file takes on
disk. But if you want to read the file into memory, these are
the right definitions to use (pick the one that uses the same
file mode as the file mode you''re going to use).


2008年10月3日星期五09:24:53 +1000,MisterE < Mi ***** @ nimga.com>

写道:
On Fri, 3 Oct 2008 09:24:53 +1000, "MisterE" <Mi*****@nimga.com>
wrote:

>我听说这并不总是有效期:
>I hear that this isn''t always valid:



你听错了。

You heard right.


>
FILE * in;
长尺寸;
in = fopen(" foo.bar"," rb");
>
FILE *in;
long size;
in = fopen("foo.bar","rb");



你打开二进制文件。

You open the file in binary.


> fseek(in,0,SEEK_END);
>fseek(in,0,SEEK_END);



标准明确指出二进制流不需要

有意义地支持具有SEEK_END值的fseek调用。

The standard specifically states "A binary stream need not
meaningfully support fseek calls with a whence value of SEEK_END."


> size = ftell(in);
fseek(in,0,SEEK_SET);

然后fread大小很多字节到内存。

显然fseek不能保证工作因为0xFF EOF或其他
>size = ftell(in);
fseek(in,0,SEEK_SET);

then fread size many bytes into memory.

Apparently fseek is not guaranteed to work because of 0xFF EOF or other



我不是你想到的。 0xFF不是二进制文件中的特殊字符

。它甚至可以是一个正常的可打印字符,因为

标准不要求ASCII或EBCDIC。 EOF不是

字符。这是一个宏。该宏中使用的值

完全有可能无法表示为char。

I don''t where you came up with this. 0xFF is not a special character
in a binary file. It could even be a normal printable character since
the standard does not mandate ASCII or EBCDIC. EOF is not a
character. It is a macro. It is entirely possible that the value
used in that macro is not representable as a char.


>字符,这是真的吗在文本模式下还是在二进制模式下?无论如何都要获取文件大小而不必一次读取字节数。它是否最好只是在它失败之前畏缩?
>characters, is this true only in text mode or also in binary mode? Is there
anyway to get a filesize without having to read bytes on at a time. Is it
best to just fread until it fails?



取决于便携性对您的重要性。


-

删除del for电子邮件

Depends on how important portability is to you.

--
Remove del for email


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

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