确定文件大小 [英] Determining size of file

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

问题描述

是否有任何C库函数返回给定文件的大小?

否则,是否有一种方法可以在C中确定文件大小
程序?我需要为Linux和Windows平台提供这个,所以我正在寻找一个

通用解决方案。


感谢您的帮助。 />

Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.

Thanks for your help.

推荐答案

2005-11-30,googler< pi ******** @ yahoo.com>写道:
On 2005-11-30, googler <pi********@yahoo.com> wrote:
是否有任何C库函数返回给定文件的大小?
否则,有没有办法在C
程序中确定文件大小?我需要为Linux和Windows平台提供这个,所以我正在寻找一个通用的解决方案。

感谢您的帮助。
Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.

Thanks for your help.




获得具有普遍意义的结果的最佳机会可能是使用getc()和使用ftell来获取整个文件的
。这可能不是最好的解决方案。在那些特定的平台上,fseek()到最后

可能会有效[有一个论据,基于理由,

它总会给你至少as作为getc()

循环的有意义的答案,至少在以二进制模式打开的文件上。两者都包括

库函数,它们会给你答案,但它们当然是每个平台上不同的



Probably the best chance at getting a universally meaningful result is
eating the entire file with getc() and using ftell. That is likely not
the best solution. On those particular platforms, fseek() to the end
will probably work [there is an argument, based on the rationale, that
it will always give you at least as meaningful an answer as the getc()
loop], at least on a file opened in binary mode. Both also include
library functions that will give you the answer, but they are of course
different on each platform.


在文章< sl ******************** @ random.yi.org>,

Jordan Abel< jm * ***@purdue.edu>写道:
In article <sl********************@random.yi.org>,
Jordan Abel <jm****@purdue.edu> wrote:
2005-11-30,googler< pi ******** @ yahoo.com>写道:
On 2005-11-30, googler <pi********@yahoo.com> wrote:
是否有任何C库函数返回给定文件的大小?
否则,有没有办法在C
程序中确定文件大小?我需要为Linux和Windows平台提供这个,所以我正在寻找一个通用的解决方案。
Is there any C library function that returns the size of a given file?
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.


获得一个具有普遍意义的结果的最好机会可能是
用getc()和ftell吃整个文件。这可能不是最好的解决方案。在那些特定的平台上,fseek()到最后
可能会有效[有一个论据,基于理由,它总是会给你至少与getc()一样有意义的答案
循环],至少在以二进制模式打开的文件上。


"二进制流无需支持fseek调用

a,值为SEEK_END"


在Linux上,没有寻求以/ dev / random或/ dev / zero

或套接字或命名管道结束;也许不是tty'或ptty'的。

读到最后将适用于套接字和命名管道......但如果

你有两个 - 方式流程进行通信然后你可能会结束逻辑死锁
...一些Unices支持无限期可扩展的

mmap()段,通过尝试读取扩展那里的数据...

两者还包括将为您提供答案的库函数,但它们在每个平台上都是不同的。

Probably the best chance at getting a universally meaningful result is
eating the entire file with getc() and using ftell. That is likely not
the best solution. On those particular platforms, fseek() to the end
will probably work [there is an argument, based on the rationale, that
it will always give you at least as meaningful an answer as the getc()
loop], at least on a file opened in binary mode.
"A binary stream need not meaningfully support fseek calls with
a whence value of SEEK_END".

On Linux, there is no seek to end for /dev/random or /dev/zero
or sockets or named pipes; possibly not for tty''s or ptty''s either.
Reading to the end would work for sockets and named pipes... though if
you have two-way process communication going on then you might end up
with a logical deadlock... Some Unices support an indefinitely-extensible
mmap() segment, extended by trying to read the data there...
Both also include
library functions that will give you the answer, but they are of course
different on each platform.




如果你不知道你所交的是普通档案

那么就很难定义一个有意义的 ;大小"为了它。但是

当然标准C库中没有任何内容可以让你测试你所传递的内容是否是普通文件。或不是。

-

没有人有权通过要求经验证据来摧毁另一个人的信念。 ; - Ann Landers



If you don''t know that what you''ve been handed is an "ordinary file"
then it can be difficult to define a meaningful "size" for it. But
of course there is nothing in the standard C library that would allow
you to test whether what you''ve been handed is an "ordinary file" or not.
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers


>是否有任何C库函数返回给定文件的大小?
>Is there any C library function that returns the size of a given file?
否则,是否有办法哪个文件大小可以在C />程序中确定?我需要在Linux和Windows平台上获得这个,所以我正在寻找一个通用的解决方案。


你想用什么数据?

感谢您的帮助。
Otherwise, is there a way in which file size can be determined in a C
program? I need to get this for both Linux and Windows platforms, so a
generic solution is what I am looking for.
What do you want to use the data for?
Thanks for your help.




* WHICH *给定文件的大小?有几个不同的定义,

并非全部列在这里,可能会给出不同的答案:


(1)你可以阅读的字符数fgetc()来自文件

在文本模式下打开时。 (在一个行结尾为\\ n的系统上,

将\\\\ n计为一个字符,但在磁盘上它是两个(MS-DOS和
Windows)。如果文件确实包含二进制内容,在某些系统上

a control-Z被视为文件结尾及其后面的字节

不算数。如果你想在文本模式下将整个文件读入内存

,这就是你想要的尺寸。


(2)当它以二进制模式打开时,你可以用文件中的fgetc()读取的字符数

。这可能会计算

结尾的字节数。写入磁盘块末尾的最后一个字节的文件

(例如,在CP / M上,它不跟踪文件结尾到字节边界)。

如果您有想要以二进制

模式将整个文件读入内存,这是您想要的大小。它也可能是大小
$ b $ l由ls -l给出。


(3)磁盘数量存储文件所需的空间。这往往是
是磁盘上数据的大小四舍五入到块边界。

关于是否计算间接
块用于跟踪属于该文件的块。

带有未写入漏洞的文件可以使(3)比(2)大得多(> 2)(例如带有不成文孔的多GB文件

适合软盘)。


(1)和(2)你可以通过打开文件,将其读到最后,

和计算字符来获得。 (3)可以通过某些系统上的非标准C

函数stat()获得(st_blocks乘以磁盘

块大小)。 stat()也可能经常在st_size中给你(2)。


Gordon L. Burditt



*WHICH* "size of a given file"? There are several different definitions,
not all listed here, which are likely to give different answers:

(1) The number of characters you can read with fgetc() from the file
when it is opened in text mode. (On a system with line endings \r\n,
this counts \r\n as one character, but on disk it''s two (MS-DOS and
Windows). If the file really contains binary stuff, on some systems
a control-Z is treated as end-of-file and it and the bytes after it
don''t count. If you had ideas of reading the whole file into memory
in text mode, this is the size you want.

(2) The number of characters you can read with fgetc() from the file
when it is opened in binary mode. This may count bytes at the
end of the file from the last byte written to the end of a disk block
(e.g. on CP/M, which doesn''t track end-of-file to a byte boundary).
If you had ideas of reading the whole file into memory in binary
mode, this is the size you want. It''s also likely to be the size
given by "ls -l".

(3) The amount of disk space needed to store the file. This tends to
be the size of the data on disk rounded up to a block boundary.
There are variations on this as to whether to count "indirect"
blocks used to keep track of blocks belonging to the file.
Files with unwritten "holes" can make (3) drastically smaller
than (2) (e.g. the multi-gigabyte file with unwritten holes
that fits on a floppy).

(1) and (2) you can get by opening the file, reading it to the end,
and counting characters. (3) can be obtained by non-standard-C
function stat() on some systems (st_blocks multiplied by the disk
block size). stat() may often give you (2) in st_size also.

Gordon L. Burditt


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

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