时间: 2021-07-31 作者:daque
这是一个对于文献正片的例程,蓄意有人不妨用上,不必再探求了。#include <stdio.h>#include <sys\stat.h>bool forceexist;const bufsize = 409600; //基础单元为400k//---------------------------------------------------------------------------void __fastcall tform1::formcreate(tobject *sender){ edit1->text = extractfilepath(paramstr(0));}//---------------------------------------------------------------------------void __fastcall tform1::button1click(tobject *sender){ if (opendialog1->execute()) { ansistring ffile = opendialog1->filename; ansistring tfile = edit1->text+extractfilename(ffile); if (copyfile(ffile,tfile)) { //复制胜利.... } else { forceexist = false; deletefile(tfile); } }}//---------------------------------------------------------------------------bool tform1::copyfile(ansistring fromfile,ansistring tofile){ bool result(false); file *fromf,*tof; int numread,numwritten; /* bufsiz is defined in stdio.h */ char buf[bufsize]; screen->cursor = crhourglass; tof = fopen(tofile.c_str(),"wb"); fromf = fopen(fromfile.c_str(),"rb"); try { starttime = now(); struct stat statbuf; /* get information about the file */ fstat(fileno(fromf), &statbuf); int modval = statbuf.st_size % bufsize; int maxval = statbuf.st_size / bufsize; if (modval) maxval++; progressbar1->max = maxval; float fsize = statbuf.st_size; tvarrec args[2] = {0,fsize}; lblfilesize->caption = format("文献巨细: %.*n字节", args, 1); lblstarttime->caption = "发端功夫: "+formatdatetime("hh:nn:ss",starttime); forceexist = false; progressbar1->position = 0; do { if (progressbar1->position<maxval-1) { numread = fread(&buf, sizeof(buf), 1, fromf); // read the data numwritten = fwrite(&buf, sizeof(buf), 1, tof); // write the data fsize = bufsize * (progressbar1->position + 1); } else if (modval) {//当结余的数据不及400k时,需作特出处置... char *smallbuf; smallbuf = (char *)sysgetmem(modval); numread = fread(smallbuf, modval, 1, fromf); // read the data numwritten = fwrite(smallbuf, modval, 1, tof); // write the data fsize += modval; sysfreemem(smallbuf); numread = 0; } progressbar1->position++; tvarrec args[2] = {0,fsize}; lblfiletrans->caption = format("仍旧复制: %.*n字节", args, 1); lblelapsetime->caption = "已用功夫: "+formatdatetime("hh:nn:ss",now()-starttime); application->processmessages(); } while (!forceexist && (numread!=0 || numread==numwritten)); progressbar1->position++; fclose(tof); fclose(fromf); result = !forceexist; } catch(...) { fclose(tof); fclose(fromf); messagedlg("在复制文献时展示缺点,未能实行操纵。", mtwarning, tmsgdlgbuttons() << mbok, 0); } screen->cursor = crdefault; return result;}//---------------------------------------------------------------------------void __fastcall tform1::btnstopclick(tobject *sender){if (messagedlg("正在复制文献,能否要废除操纵?", mtwarning, tmsgdlgbuttons() << mbyes << mbno, 0)==mryes) forceexist = true;}//---------------------------------------------------------------------------