首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言获取文件大小解决思路

2012-09-22 
C语言获取文件大小想用c语言获取png文件大小,用二进制方式打开,采用了以下几种方法均出错:1.fseek+ftell2.

C语言获取文件大小
想用c语言获取png文件大小,用二进制方式打开,采用了以下几种方法均出错:
1.fseek+ftell
2.stat
3.filelength

请问有没有其他方法可以获取例如.zip .rar .png之类特殊格式文件的大小?

[解决办法]
你用的不对吧
fseek(fp, 0, SEEK_END);
length = ftell(fp);
就可以了
[解决办法]
FILE *f;
__int64 fl;
f=fopen(...,"rb");
fl=_filelengthi64(_fileno(f));
fclose(f);
printf("%I64d\n",fl);

[解决办法]
_stat, _wstat, _stati64, _wstati64
Get status information on a file.
=====================================
GetFileSize
The GetFileSize function retrieves the size of a specified file. 

This function stores the file size in a DWORD value. To retrieve a file size that is larger than a DWORD value, use the GetFileSizeEx function. 

DWORD GetFileSize(
HANDLE hFile, // handle to file
LPDWORD lpFileSizeHigh // high-order word of file size
);



[解决办法]
方法多种多样。。。GetFileSize 这个最简单!!
[解决办法]
#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, 
http://blog.csdn.net/xiongchen/article/details/661400

热点排行