您的位置:首页 > 其它

标准C获得文件大小的5种方法

2009-08-26 19:39 507 查看
2009-08-26 16:46
#include "stdafx.h"
#include <sys/stat.h>
#include <io.h>
#include <FCNTL.H>

int getfilesize()
{
int iresult;
struct _stat buf;
iresult = _stat(__FILE__,&buf);
if(iresult == 0)
{
return buf.st_size;
}
return NULL;
}

int getfilesize01()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _filelength(fp);
//return NULL;
}

int getfilesize02()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _lseek(fp,0,SEEK_END);
//return NULL;
}

int getfilesize03()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _lseek(fp,0,SEEK_END);
//return NULL;
}

int getfilesize04()
{
FILE *fp;
if((fp=fopen(__FILE__,"r"))==NULL)
return 0;
fseek(fp,0,SEEK_END);
return ftell(fp); //return NULL;
}

int getfilesize05()
{
FILE *fp;
char str[1];
if((fp=fopen(__FILE__,"rb"))==NULL)
return 0;
for(int i = 0;!feof(fp);i++)
{
fread(&str,1,1,fp);

}
return i - 1; //return NULL;
}

int main(int argc, char* argv[])
{

printf("getfilesize()=%d/n",getfilesize());
printf("getfilesize01()=%d/n",getfilesize01());
printf("getfilesize02()=%d/n",getfilesize02());
printf("getfilesize03()=%d/n",getfilesize03());
printf("getfilesize04()=%d/n",getfilesize04());
printf("getfilesize05()=%d/n",getfilesize05());
return 0;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: