菜鸟提问:C++实现文件读写速度测试迷茫?
本人想要在嵌入式平台下测试SD卡中文件读写速度测试,设想把SD卡内的文件读或是写到一个缓冲区中buffer后丢弃,不断读写直到整个文件读写完成,计算整个过程使用的时间,文件大小已知就可以得出速度大小。本人尝试两种方法都存在问题,现把代码贴出来,希望大家给我看看那里错了,本人不胜感激!
FILE* file = NULL ;
DWORD Tick1,Tick2,Tick3,Tick4;
CHAR buf[512];
file = fopen("\\Storage Card\\10M.bin\\10M.bin", "r+t");
Tick1=GetTickCount(); // gets current time
if(file)
{
int num = 1;
while (num)
{
memset(buf,0,512);
num = fread(buf,sizeof(CHAR),512,file);
fclose( file );
Tick2=GetTickCount()-Tick1;
RETAILMSG(1,
(TEXT("shijiancha1 = %d \n"),Tick2));
}
}
本程序测试时不能实现整个文件读的。本人又模仿msdn编写下面一个,还是不行。
FILE *stream;
DWORD Tick1,Tick2,Tick3,Tick4;
char list[512];
int i, numread, numwritten;
CFile strFileR;
/* Open file in text mode:*/
if( (stream = fopen("\\Storage Card\\10M.bin", "r+t" )) != NULL )
{
Tick1=GetTickCount(); // gets current time
/* Write 512 characters to stream */
numwritten = fwrite(list, sizeof(list), strFileR.GetLength()+1, stream );
fclose( stream );
Tick2=GetTickCount()-Tick1;
RETAILMSG(1,
(TEXT("shijiancha1 = %d \n"),Tick2));
}
else
printf( "Problem opening the file\n" );
if( (stream = fopen("\\Storage Card\\10M.bin", "r+t" )) != NULL )
{
Tick4=GetTickCount(); // gets current time
/* Attempt to read in 512 characters */
numread = fread(list, sizeof(list), strFileR.GetLength()+1, stream );
RETAILMSG(1,
(TEXT("numread = %d \n"),numread));
RETAILMSG(1,
(TEXT("list = %d \n"),list));
fclose( stream );
Tick3=GetTickCount()-Tick4;
RETAILMSG(1,
(TEXT("shijiancha3 = %d \n"),Tick3));
}
else
printf( "File could not be opened\n" );
l两个程序反馈回来的和我实际设想的差距太大,结果存在明显错误,劳烦大家帮我看看,提供宝贵建议,再次感谢!