Procedure code (C ++ Builder) for file replication

zhaozj2021-02-16  46

This is a routine about the file copy, I hope someone can use it, no need to explore it.

#include #include

Bool forceexist; const bufsize = 409600; // Basic unit is 400K

/ / -------------------------------------------------------------------------------------------- --------------------------- void __fastcall tform1 :: formcreate (TOBJECT * Sender) {Edit1-> Text = extractFilePath (paramstr (0) } // --------------------------------------------- ------------------------------ Void __fastcall tform1 :: butt * sender {ix (OpenDialog1-> Execute () ) {ANSISTRING FFILE = OpenDialog1-> FileName; Ansistring Tfile = Edit1-> Text ExtractFileName (ffile); if (copyfile (ffile, tfile) {

// Copy success ....} 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 [buff ";

Screen-> Cursor = CRHOURGLASS; TOF = FOPEN (Tofile.c_STR (), "WB"); fromf = fopen (fromfile.c_str (), "RB"); try {starttime = now (); struct stattbuf; / * 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 ("file size:%. * n bytes", args, 1); lblstarttime-> caption = "start time : " FormatorTime (" HH: NN: SS ", StartTime);

Forceexist = false; progressbar1-> position = 0; do {if (progressbar1-> position Position 1);} Else if (modval) {// When the remaining data is less than 400K, it is necessary to special Handling ... char * smallbuf; smallbuf = (char *) sysgetMem (MODVAL); NumRead = FREAD (Smallbuf, Modval, 1, fromf); // read the data number number = fwrite (Smallbuf, ModVal, 1, TOF); // Write the data fsize = modval; sysfreemem (smallbuf); Numread = 0;} progressbar1-> position ; tvarRec args [2] = {0, fsize}; lblfileTrans-> caption = format ("has been replicated:%. * n bytes ", args, 1); lblelapsetime-> caption =" has been used: " FormatorTime (" hh: nn: ss ", now () - start-> processMessages ();} while ! Forceexist && (Numread! = 0 || Numread == NumWritten); ProgressBar1-> Position ; fclose (TOF); fclose (fromf); result =! Forceexist;} catch (...) {fclose (TOF); Fclose (fromf); Messagedlg ("" An error occurred while replicating files, failed to complete.

转载请注明原文地址:https://www.9cbs.com/read-26932.html

New Post(0)