我正在使用vs2008 win xp x32并使用一些函数读取二进制文件,然后在其他txt文件中放入一些统计信息:
这是功能:
然后我会添加行
ofstream filestr(pathname,ios::out);
函数运行速度快约3倍(21秒vs约250Mb文件75秒)WTF?
__int64 ReadBinaryFile(const char* fileName)
{
char* pathname= "Stats.txt";
ofstream filestr(pathname,ios::out); //comment/uncomment this line
FILE* f = fopen(fileName, "rb");
if(f != NULL)
{
if( _fseeki64(f,0,SEEK_END) != 0) // 0 == success
return 0;
__int64 file_size = _ftelli64(f);
if (file_size == 0)
return 0;
if( _fseeki64(f,0,SEEK_SET) != 0) // 0 == success
return 0;
while( !feof( f ) )
{
unsigned char temp[2];
if(fread(temp,sizeof(unsigned char),2,f)!=2)
break;
int skip_bytes= LowHighByte(temp[0],temp[1]); //skip n bytes
if (skip_bytes==0)
break;
if(fread(temp,sizeof(unsigned char),2,f)!=2)
break;
int marker= LowHighByte(temp[0],temp[1]);
int sz= skip_bytes-4;
unsigned char* data= new unsigned char[sz];
if(fread(data,sizeof(unsigned char),sz,f)!= sz )
break;
delete[] data;
}
filestr.close();
return 1;
}
return 0;
}