window系统下直接读取文件大小

大家都知道,如果使用文件读取获取文件大小,需要流处理至最后一个字符才能确定文件大小。
fseek() 至文件末尾,再求文件的长度。这样效率很低。
于是乎,我就利用操作系统的文件信息直接得到文件大小。这里是文件本身的大小,不是文件占用的系统内空间大小。


#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <time.h>
//#include <sys/timeb.h>
#include <sys\stat.h>
#include <io.h>

using namespace std;

void main()
{
	char *filepath = "S:/迅雷下载/心花路放.rmvb";

	clock_t start,end;

	start=clock();
	struct _stat info;
	_stat(filepath, &info);
	int size = info.st_size;
	end=clock();
	cout<<size<<endl;
	printf("The differences: %d ms\n",end-start);

	int i;
	cin >> i;
}

你可能感兴趣的:(window系统下直接读取文件大小)