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

由于毕业设计中一个模块需要获取文件大小,但是标准库函数并未提供对应的实现,所以从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,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值