SIze的文件 [英] SIze of file

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

问题描述

大家好,


我想知道文件的大小(txt,img或任何其他文件)。我知道

只有文件名。

我怎么能实现这一点。

是否有人对此有所了解。

PLZ帮助。


rgrds,

Munish Nayyar

Hi All,

I want to know the size of file (txt,img or any other file). i knoe
only file name.
how i can acheive this.
does anybody is having idea about that.
plz help.

rgrds,
Munish Nayyar

推荐答案

off_t fgetstat(const char * pathname)

{

struct stat * stbuf;


if(lstat( pathname,stbuf)== -1){

fprintf(stderr,lstat%s failed:%s \ n",pathname,strerror(errno));

退出(EXIT_FAILURE);

}

返回stbuf.st_size;

}

off_t fgetstat(const char * pathname)
{
struct stat * stbuf;

if (lstat(pathname, stbuf) == -1) {
fprintf(stderr, "lstat %s failed: %s\n", pathname, strerror(errno));
exit(EXIT_FAILURE);
}
return stbuf.st_size;
}



mu*******@gmail.com 写道:
大家好,

我想知道文件的大小(txt,img或任何其他文件)。我只知道文件名。
我怎么能实现这个目标。
是否有人对此有所了解。
PLZ帮助。

rgrds,< Munish Nayyar
Hi All,

I want to know the size of file (txt,img or any other file). i knoe
only file name.
how i can acheive this.
does anybody is having idea about that.
plz help.

rgrds,
Munish Nayyar




查看常见问题解答:

http://www.eskimo.com/~scs/C-faq/q19.12.html


-David



Have a look at the FAQ:

http://www.eskimo.com/~scs/C-faq/q19.12.html

-David


-----开始PGP签名消息-----

哈希:SHA1

mu*******@gmail.com 写道:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mu*******@gmail.com wrote:
大家好,

我想知道文件的大小(txt,img或任何其他文件)。我只知道文件名。
我怎么能实现这个目标。
是否有人对此有所了解。
PLZ帮助。
Hi All,

I want to know the size of file (txt,img or any other file). i knoe
only file name.
how i can acheive this.
does anybody is having idea about that.
plz help.



我只能想到两种方法来确定与C标准一致的文件大小:


1)fopen()文件,

将无符号长(或更长)变量初始化为0

,直到fgetc()返回文件结尾,向计数器加1

fclose()文件,





2)fopen()文件

fseek()到文件的末尾

ftell()文件结尾的位置

fclose()文件


这两种方法都有其缺点。字符计数可能会给出不同的结果

取决于文件是以二进制还是文本打开

模式,而fseek()/ ftell()可能有效也可能无效,具体取决于关于实现

(一个二进制流不需要有意义地支持fseek调用具有某个值

的SEEK_END和ftell()返回值是绝对位置(当在二进制流上使用

时,或者在文本流上使用时,某些未指定的信息)。


您的选择。


- -

Lew Pitcher

IT专家,企业数据系统,

企业技术解决方案,道明银行金融集团


(表达的意见是我自己的,不是我的雇主')

----- BEGIN PGP SIGNATURE -----

版本:GnuPG v1.2.4(MingW32)

iD8DBQFC + 1XaagVFX4UWr64RAhyzAKCYNfPqV64rRBZ5p6SoxZ oJxMvp5gCg7C80

colQvhV5MvyjLy / zhrNDSgA =

= CstA
----- END PGP SIGNATURE -----



I can only think of two methods to determine file size that are consistant with
the C standard:

1) fopen() the file,
initialize an unsigned long (or longer) variable to 0
until fgetc() returns end of file, add 1 to the counter
fclose() the file,

or

2) fopen() the file
fseek() to the end of the file
ftell() the position of the end of the file
fclose() the file

Both of these methods have their drawbacks. Character counting may give
different results depending on whether the file was opened in binary or text
mode, and fseek()/ftell() may or may not work, depending on the implementation
("a binary stream need not meaningfully support fseek calls with a whence value
of SEEK_END" and ftell() return value is either the absolute position (when used
on a binary stream), or some "unspecified information" when used on a text stream).

Your choice.

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers'')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFC+1XaagVFX4UWr64RAhyzAKCYNfPqV64rRBZ5p6SoxZ oJxMvp5gCg7C80
colQvhV5MvyjLy/zhrNDSgA=
=CstA
-----END PGP SIGNATURE-----


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

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