文件大小 [英] Size of a File

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

问题描述



我是C编程的新手并且被困在某个地方。

我想找到文件的大小。

我找不到合适的方式。

我打算做的是......

打开请求的文件,

默认位置是第一个字节。

现在我使用fseek()并将其移动到文件的发送。

比我使用ftell()获取文件中的当前位置。


这应该返回文件的字节数(长度/大小)。


是这是一个正确的方法吗?

有没有有效的方法呢?


谢谢

Hi,
I ''m new to C programming and ''m stuck somewhere.
I want to find the size of a file.
I couldn''t find a proper way of doing it.
What I was planning to do is...
Open the requested file,
default position is 1st byte.
Now I use fseek() and move it till the send of the file.
Than I use ftell() to get the current position in the file.

this should return the num of bytes (length / size) of the file.

Is this a proper way to do?
Is there any efficient way to do so?

Thanks

推荐答案

Ashit Vora写道:
Ashit Vora wrote:



我是C编程的新手并且'被困在某个地方。

我想找到一个文件的大小。

我找不到合适的方法。

我打算做的是...

打开请求的文件,

默认位置是第1个字节。

现在我使用fseek()并移动它直到发送文件。 />
我使用ftell()来获取文件中的当前位置。
Hi,
I ''m new to C programming and ''m stuck somewhere.
I want to find the size of a file.
I couldn''t find a proper way of doing it.
What I was planning to do is...
Open the requested file,
default position is 1st byte.
Now I use fseek() and move it till the send of the file.
Than I use ftell() to get the current position in the file.



这也是我会做的事情(当我有一段时间同样的问题时我被建议做什么?
以前)。


我不知道其他任何方式(做便携)。可能有

更好但是使用平台特定API(如POSIX)的技巧。


Daniel


-

完成:Arc- Bar-Cav-Sam-Val-Wiz,Dwa-Elf-Gno-Hum-Orc,Law-Neu-Cha,Fem-Mal

去:Hea-Kni-Mon-Pri-Ran- Rog-Tou

That''s what I would do, too (and what I was suggested to do when I had
the same problem some time ago).

I don''t know of any other way (to do it portably). There may be
"better" tricks using platform specific APIs (like POSIX) though.

Daniel

--
Done: Arc-Bar-Cav-Sam-Val-Wiz, Dwa-Elf-Gno-Hum-Orc, Law-Neu-Cha, Fem-Mal
To go: Hea-Kni-Mon-Pri-Ran-Rog-Tou


>我是C编程的新手并且被困在某个地方。
>I ''m new to C programming and ''m stuck somewhere.

>我想找到一个文件的大小。
>I want to find the size of a file.



首先,你需要定义* IS *文件的大小。

这不是一个小问题。


这是它在磁盘上占用的空间量,包括部分未用的

块?这包括目录槽的大小吗? Inode?

是否可以在二进制模式下从文件读取的字节数?

是否可以在文本模式下从文件中读取的字节数? (这个

可能与Windows系统中的上述内容有所不同,有\\ n行

结尾)。

是不是UNIXls -l的字节数或WindowsDIR

命令返回该文件?

如果文件大小在
$之间变化会发生什么不好的事情b $ b你计算文件大小的时间和你使用的时间

数(为什么?)?

First of all, you need to define what *IS* the size of a file.
It''s not a trivial question.

Is it the amount of space it takes on disk, including partially unused
blocks? Does that include the size of a directory slot? Inode?
Is it the number of bytes you can read from the file in binary mode?
Is it the number of bytes you can read from the file in text mode? (This
is likely to differ from the above on Windows systems with \r\n line
endings).
Is it the number of bytes that the UNIX "ls -l" or Windows "DIR"
command returns for that file?

What bad things will happen if the size of the file changes between
the time you compute the size of the file and the time you use that
number (for what?)?


> ;我找不到合适的方法。
>I couldn''t find a proper way of doing it.



根据您对文件大小的定义,打开

文件(以二进制或文本模式;您使用的模式可能影响

答案),读取和计算字符将起作用。

Depending on your definition of the size of a file, opening the
file (in binary or text mode; which mode you use may affect the
answer), and reading and counting characters will work.


>我打算做的是...
打开请求的文件,
默认位置是第1个字节。
现在我使用fseek()并移动它直到发送文件。
>What I was planning to do is...
Open the requested file,
default position is 1st byte.
Now I use fseek() and move it till the send of the file.



问题:二进制文件无需支持在文件末尾搜索

。例如,在CP / M中,

二进制文件的大小是块大小的倍数,并且它不会跟踪b
你写的最后一个块。

Problem: a binary file need not meaningfully support seeking
to the end of the file. In CP/M, for example, the size of a
binary file is a multiple of the block size, and it doesn''t
keep track of how far into the last block you''ve written.


>比我使用ftell()来获取文件中的当前位置。
>Than I use ftell() to get the current position in the file.



问题:对于文本文件,该位置不必是任何数字的数字

。它可能是扇区,头部,柱面,

轨道,火车,磁盘编号等的位域,与数字无关。

字节。

Problem: For a text file, the position need not be a number
of anything. It could be a bitfield of sector, head, cylinder,
track, train, disk number, etc. that has no correlation to a number
of bytes.


>这应该返回文件的字节数(长度/大小)。

这是一种正确的方法吗?
>this should return the num of bytes (length / size) of the file.

Is this a proper way to do?



它在ANSI C下不可移植。

It''s not portable under ANSI C.


>有没有有效的方法这样做?
>Is there any efficient way to do so?



没有便携,有效的方法。 stat(),如果可用,

可能比你的方法更有效。打开文件,阅读

,计算字节可能效率很低。

There isn''t a portable, efficient way to do so. stat(), if available,
might be more efficient than your method. Opening the file, reading
it, and counting bytes may be very inefficient.


2008年9月7日16:04 ,在comp.lang.c中,Gordon Burditt

(go***********@burditt.org)写道:
On September 7, 2008 16:04, in comp.lang.c, Gordon Burditt
(go***********@burditt.org) wrote:

>>我是C编程的新手并且被困在某处。
我想找到一个文件的大小。
>>I ''m new to C programming and ''m stuck somewhere.
I want to find the size of a file.



首先,你需要定义* IS *文件的大小。

这不是一个小问题。


First of all, you need to define what *IS* the size of a file.
It''s not a trivial question.



同意

Agreed


这是它在磁盘上占用的空间量,包括部分未使用的

块?这包括目录槽的大小吗? Inode?

是否可以在二进制模式下从文件读取的字节数?

是否可以在文本模式下从文件中读取的字节数? (这个

可能与Windows系统中的上述内容有所不同,有\\ n行

结尾)。

是不是UNIXls -l的字节数或WindowsDIR

命令返回该文件?
Is it the amount of space it takes on disk, including partially unused
blocks? Does that include the size of a directory slot? Inode?
Is it the number of bytes you can read from the file in binary mode?
Is it the number of bytes you can read from the file in text mode? (This
is likely to differ from the above on Windows systems with \r\n line
endings).
Is it the number of bytes that the UNIX "ls -l" or Windows "DIR"
command returns for that file?



它是稀疏文件中存储的字节数的计数(其中,
是文件中没有数据的区域)所有)或者是虚拟的

这个稀疏文件的大小,其间插入的空白点被认为具有

数据? (注意,至少在unix中,ls命令显示虚拟大小,

而不是写入稀疏文件的实际数据的计数。)


[snip]

-

Lew Pitcher


Master Code代理& JOAT-in-training |已注册的Linux用户#112576
http://pitcher.digitalfreehold.ca/ |可根据要求提供GPG公钥

---------- Slackware - 因为我知道我在做什么。 ------

Is it the count of the number of bytes stored in a sparse file (where there
are areas of the file with no data at all) or is it the "virtual" size of
the sparse file, where the intervening empty spots are presumed to have
data? (Note, at least in unix, the "ls" command shows the "virtual" size,
rather than a count of the real data written to a sparse file.)

[snip]
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I''m doing. ------


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

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