*/
#include "stdio.h"
main()
{ FILE *fp /*定义文件指针*/
long cnt /*定义变量cnt用来存储文件大小数据*/
fp=fopen("c:\\1.txt","r") /*此处请填写测试文件的地址,并以只读("r")方式打开文件*/
if( fp==NULL ) /*如果打开的文件不存在,则退出*/
{ exit(0)
printf("文件打开失败!")
}
fseek(fp,0L,SEEK_END) /*利用fseek函数将指针定位在文件结尾的位置*/
cnt=ftell(fp) /*利用ftell函数返回指针相对于文件开头的位置,以字节计算*/
printf("文件所占的字节数为%ld个\n",cnt) /*进行输出*/
}
c语言可以通过stat()函数获得文件属性,通过返回的文件属性,从中获取文件大小。#include
<sys/stat.h>
可见以下结构体和函数
struct
stat
{
_dev_t
st_dev
_ino_t
st_ino
unsigned
short
st_mode
short
st_nlink
short
st_uid
short
st_gid
_dev_t
st_rdev
_off_t
st_size
//文件大小
time_t
st_atime
time_t
st_mtime
time_t
st_ctime
}
stat(const
char
*,
struct
_stat
*)
//根据文件名得到文件属性
参考代码:
#include <sys/stat.h>
void main( )
{
struct stat buf
if ( stat( "test.txt", &buf ) <0 )
{
perror( "stat" )
return
}
printf("file size:%d\n", buf.st_size )
}