加了进度条速度慢了许多,怎么办?
按字节复制文件,希望用一个进度条来显示复制进度,发现加了进度条后,复制速度比原来慢了N倍,知道是中间计算量增加的问题,但是怎么改才能让它跟原来一样呢?代码如下:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "stdio.h"
//----------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
long GetFileLen(FILE *file)
{
fseek(file,0,2);
return ftell(file);
}
TForm1 *Form1;
//----------------------------------------------------------------------fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//----------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE *fp1,*fp2;
long filelen;
fp1=fopen("1.dat","rb");
fp2=fopen("2.dat","ab");
Button1->Caption=GetFileLen(fp1);
ProgressBar1->Max=GetFileLen(fp1)/(1048576);
ProgressBar1->Position=0;
fseek(fp1,0,0);
int c;
while((c=fgetc(fp1))!=EOF)
{
fputc(c,fp2);
Application->ProcessMessages();
if (ftell(fp1)%(1048576)==0)
{
ProgressBar1->Position=ProgressBar1->Position+1;
}
}
fclose(fp1);
fclose(fp2);
Form1->Close();
}
//----------------------------------------------------------------------