文件大小... [英] file size...

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

问题描述

确定文件大小的最佳方法是什么?


我应该使用stat吗?


这样更好方式?


std :: fstream

file(" myfile",std :: ios_base :: in | std :: ios_base :: binary);

file.seekg(0,std :: ios_base :: end);

unsigned long length = file.tellg();

file .close();


有没有更多有用的方法?无论我决定

,都必须是便携式的。我的程序至少需要在Windows上运行

(x86),Linux(x86)和Mac OS X(ppc)。


谢谢,


- 约翰拉特利夫

解决方案

John Ratliff写道:

什么是确定文件大小的最佳方法是什么?


没有最佳。每种方式都有其优点和缺点。

我应该使用stat吗?


我不知道。我工作的平台没有统计。它是

不是标准功能。

这是一个更好的方法吗?

std :: fstream
文件(" ; myfile",std :: ios_base :: in | std :: ios_base :: binary);
file.seekg(0,std :: ios_base :: end);
unsigned long length = file。 tellg();
file.close();


不会。这只是一种不同的方式。

还有更多可能更有用的方法吗?无论我决定做什么都必须是便携式的。我的程序至少需要处理Windows(x86),Linux(x86)和Mac OS X(ppc)。




你可能是最好使用Boost的设施进行目录

访问。


V


Victor Bazarov写道:

John Ratliff写道:

确定文件大小的最佳方法是什么?

有没有最好的。每种方式都有其优点和缺点。

我应该使用stat吗?



我不知道。我工作的平台没有统计。它不是标准功能。




真的。我以为每个人都有统计数据我认为它是

标准C库的一部分。我知道这是在Windows,Linux和Mac OS X上,因为

这就是我以前做的事情。但是我认为可能会有一些东西可以更好地工作,或者至少可以移植到任何

ISO C ++安装中。

你做什么没有统计数据?

这是一个更好的方法吗?

std :: fstream
file(" myfile",std :: ios_base :: in | std :: ios_base :: binary);
file.seekg(0,std :: ios_base :: end);
无符号long length = file.tellg();
file.close();

不,这只是一种不同的方式。




:-)我应该知道比使用''best''和

''更好'这样的词更好。让我重新说一下。这种方法有什么缺点吗?什么

如果用户意外打开了20 GB的文件。以这种方式计算尺寸需要花费很长时间吗?或者是在恒定时间内进行搜索吗?

有没有更多可能更有用的方法?无论我决定做什么都必须是便携式的。我的程序至少需要处理Windows(x86),Linux(x86)和Mac OS X(ppc)。



你可能最好使用Boost目录
访问的设施。




对于单一功能来说似乎有点矫枉过正。


我的目的很简单。用户不应该尝试打开

长度不是0x2000字节的文件。因此,如果他们不小心选择了一个

不是0x2000字节,程序就不应该打开它了。


谢谢,


- John Ratliff


John Ratliff写道:


真的。我以为每个人都有统计数据我认为它是
标准C库的一部分。我知道这是在Windows,Linux和Mac OS X上,因为它是我以前做的事情。但是我认为C ++标准中可能会有更好的工作,或者至少可以移植到任何
ISO C ++安装中。

你在做什么工作没有统计数据?


也许是一个嵌入式系统,一个用于微波炉的微控制器:-)

(是的:这些东西有C和C ++编译器)

:-)我应该知道比使用''best''和
''better''这样的词更好。让我重新说一下。这种方法有什么缺点吗?什么
如果用户意外打开了20 GB的文件。是否需要花费很长时间才能以这种方式计算尺寸,或者是否需要在恒定时间内进行搜索?


没人能说肯定,你需要在你的系统上试试。


还有更多可能更有用的方法吗?无论我决定做什么都必须是便携式的。我的程序至少需要处理Windows(x86),Linux(x86)和Mac OS X(ppc)。



你可能最好使用Boost目录
访问的设施。



对于单一功能来说似乎有点矫枉过正。

我的目的非常简单。用户不应该尝试打开长度不是0x2000字节的文件。因此,如果他们不小心选择了一个不是0x2000字节的程序,程序就不应该打开它了。




为什么是0x2000?

如果是因为你有自己的文件格式和文件格式

导致文件的大小正确,那么有更好的方法:


用一些''幻数''开始你的文件。

当涉及到阅读文件时,打开它,读取前几个字节并检查

,你的神奇数字就在那里。如果是,请继续。如果不是,那它不是你的b
文件之一。没有必要,没有必要的stat()和最好的:你在

你的文件版本控制方式。


-

Karl Heinz Buchegger
kb******@gascad.at


What is the best way to determine a file''s size?

Should I use stat?

Is this a better way?

std::fstream
file("myfile", std::ios_base::in | std::ios_base::binary);
file.seekg(0, std::ios_base::end);
unsigned long length = file.tellg();
file.close();

Are there any more ways that might be more useful? Whatever I decide to
do has to be portable. My program will need to work on at least Windows
(x86), Linux (x86), and Mac OS X (ppc).

Thanks,

--John Ratliff

解决方案

John Ratliff wrote:

What is the best way to determine a file''s size?
There is no "best". Each way has its advantages and drawbacks.
Should I use stat?
I don''t know. The platform I work on doesn''t have ''stat''. It''s
not a standard function.
Is this a better way?

std::fstream
file("myfile", std::ios_base::in | std::ios_base::binary);
file.seekg(0, std::ios_base::end);
unsigned long length = file.tellg();
file.close();
No. It''s just a different way.
Are there any more ways that might be more useful? Whatever I decide
to do has to be portable. My program will need to work on at least
Windows (x86), Linux (x86), and Mac OS X (ppc).



You might be better off using Boost''s facilities for directory
access.

V


Victor Bazarov wrote:

John Ratliff wrote:

What is the best way to determine a file''s size?

There is no "best". Each way has its advantages and drawbacks.

Should I use stat?


I don''t know. The platform I work on doesn''t have ''stat''. It''s
not a standard function.



Really. I thought everyone had stat. I thought it was part of the
standard C library. I know it''s on Windows, Linux, and Mac OS X because
it''s what I''m used to doing. But I thought there might be something in
the C++ standard that would work nicer, or at least be portable to any
ISO C++ installation.

What do you work on that doesn''t have stat?

Is this a better way?

std::fstream
file("myfile", std::ios_base::in | std::ios_base::binary);
file.seekg(0, std::ios_base::end);
unsigned long length = file.tellg();
file.close();

No. It''s just a different way.



:-) I should have known better than to use words like ''best'' and
''better''. Let me rephrase. Are there any drawbacks to this method? What
if a user accidentally opened a 20 GB file. Is it going to take a long
time to calculate the size this way, or does seekg run in constant time?

Are there any more ways that might be more useful? Whatever I decide
to do has to be portable. My program will need to work on at least
Windows (x86), Linux (x86), and Mac OS X (ppc).


You might be better off using Boost''s facilities for directory
access.



Seems like overkill for a single function.

My purpose is pretty simple. The user shouldn''t try to open files that
aren''t 0x2000 bytes in length. So if they accidentally select one that
isn''t 0x2000 bytes, the program shouldn''t bother opening it.

Thanks,

--John Ratliff


John Ratliff wrote:


Really. I thought everyone had stat. I thought it was part of the
standard C library. I know it''s on Windows, Linux, and Mac OS X because
it''s what I''m used to doing. But I thought there might be something in
the C++ standard that would work nicer, or at least be portable to any
ISO C++ installation.

What do you work on that doesn''t have stat?
Maybe an embedded system, a microcontroller for a microwave ofen :-)
(Yes: there are C and C++ compilers for such things)

:-) I should have known better than to use words like ''best'' and
''better''. Let me rephrase. Are there any drawbacks to this method? What
if a user accidentally opened a 20 GB file. Is it going to take a long
time to calculate the size this way, or does seekg run in constant time?
Nobody can say for sure, you need to try it on your system.


Are there any more ways that might be more useful? Whatever I decide
to do has to be portable. My program will need to work on at least
Windows (x86), Linux (x86), and Mac OS X (ppc).


You might be better off using Boost''s facilities for directory
access.



Seems like overkill for a single function.

My purpose is pretty simple. The user shouldn''t try to open files that
aren''t 0x2000 bytes in length. So if they accidentally select one that
isn''t 0x2000 bytes, the program shouldn''t bother opening it.



Why 0x2000 ?
If it is because you have some file format of your own and that file format
leads to files with exactly that size, then there is a better way:

Start your files with some ''magic number''.
When it comes to reading a file, open it, read the first few bytes and check
that your magic number is there. If yes, continue. If no, it wasn''t one of your
files. No seeking necessary, no stat() necessary and best of all: you are on
your way to file versioning.

--
Karl Heinz Buchegger
kb******@gascad.at


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

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