您的位置:首页 > 编程语言 > C语言/C++

C语言中获取文件大小的方法

2006-04-13 10:11 543 查看
由于毕业设计中一个模块需要获取文件大小,但是标准库函数并未提供对应的实现,所以从Linux那弄了个过来.
下面是GCC里的stat的资料:
这个是GCC中struct stat的资料
Data Type: struct stat
The stat structure type is used to return information about the attributes of a file. It contains at least the following members:
mode_t st_mode

Specifies the mode of the file. This includes file type information (see Testing File Type) and the file permission bits (see Permission Bits).
ino_t st_ino

The file serial number, which distinguishes this file from all other files on the same device.

dev_t st_dev

Identifies the device containing the file. The st_ino and st_dev, taken together, uniquely identify the file. The st_dev value is not necessarily consistent across reboots or system crashes, however.
nlink_t st_nlink

The number of hard links to the file. This count keeps track of how many directories have entries for this file. If the count is ever decremented to zero, then the file itself is discarded as soon as no process still holds it open. Symbolic links are not counted in the total.
uid_t st_uid

The user ID of the file's owner. See File Owner.

gid_t st_gid

The group ID of the file. See File Owner.

off_t st_size

This specifies the size of a regular file in bytes. For files that are really devices this field isn't usually meaningful. For symbolic links this specifies the length of the file name the link refers to.
time_t st_atime

This is the last access time for the file. See File Times.

unsigned long int st_atime_usec

This is the fractional part of the last access time for the file. See File Times.

time_t st_mtime

This is the time of the last modification to the contents of the file. See File Times.

unsigned long int st_mtime_usec

This is the fractional part of the time of the last modification to the contents of the file. See File Times.

time_t st_ctime

This is the time of the last modification to the attributes of the file. See File Times.

unsigned long int st_ctime_usec

This is the fractional part of the time of the last modification to the attributes of the file. See File Times.

blkcnt_t st_blocks

This is the amount of disk space that the file occupies, measured in units of 512-byte blocks.

The number of disk blocks is not strictly proportional to the size of the file, for two reasons: the file system may use some blocks for internal record keeping; and the file may be sparse--it may have "holes" which contain zeros but do not actually take up space on the disk.

You can tell (approximately) whether a file is sparse by comparing this value with st_size, like this:

(st.st_blocks * 512 < st.st_size)

This test is not perfect because a file that is just slightly sparse might not be detected as sparse at all. For practical applications, this is not a problem.
unsigned int st_blksize

The optimal block size for reading of writing this file, in bytes. You might use this size for allocating the buffer space for reading of writing the file. (This is unrelated to st_blocks.)
The extensions for the Large File Support (LFS) require, even on 32-bit machines, types which can handle file sizes up to 2^63. Therefore a new definition of struct stat is necessary.

LCC中也有这样的函数,很方便用
#include <sys/types.h>
#include <sys/stat.h>
int _stat( const char *path, struct _stat *buffer );

先写个测试的:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main()
{
FILE *fi;
printf("/tplease input the filename:");
scanf("%s",infile);

if((fi=fopen(infile,"w"))==NULL)
printf("/tcannot open infile/n");

struct stat st;
if(_stat(fi,&st)==0)
if(st.st_size>0xffff)
{ }
else { }

system("PAUSE");
return 0;
}

int fstat(int handle, struct _stat *buffer);
其中int handle即你FILE*的fi

如果直接用文件名作参数,
就用:
int _stat( const char *path, struct _stat *buffer );
其中path就可以直接用你scanf到的infile,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: