Unix_C 计算传入文件的大小
2016-07-06 15:26
225 查看
请转 -----> c 到 unix c 高级编程中计算文件大小的三种方法
//指定文件名计算其大小 #include <stdio.h> //自定义函数计算参数传入文件的大小 int size( char * pc ) { //1.打开文件,使用fopen函数 FILE * fp = fopen( pc, "rb" ); if( NULL == fp ) { return -1; } //2.调整文件读写位置到末尾,用fseek函数 fseek(fp, 0, SEEK_END); //3、获取文件大小并返回,用ftell函数 int res = ftell( fp ); //4、关闭文件 fclose(fp); fp = NULL; return res; } int main( int argc, char * argv[]) { if( argc != 2) { printf("程序参数个数有误!\n"); } int res = size( argv[1] ); if( res != -1 ) { printf("改文件的大小是:%d \n", res); } else { printf("文件打开失败!\n"); } return 0; }