5. Implementing File Replication
1 intCopyFile (Char*sourcefile,Char*NewFile)2 {3Ifstreaminch;4Ofstream out;5 inch. open (Sourcefile,ios::binary);//Open source File6 if(inch. Fail ())//failed to open source file7 {8cout<<"Error 1:fail to open the source file."<<Endl;9 inch. Close ();Ten out. Close (); One return 0; A } - out. open (Newfile,ios::binary);//Create a target file - if( out. Fail ())//failed to create file the { -cout<<"Error 2:fail To create the new file."<<Endl; - out. Close (); - inch. Close (); + return 0; - } + Else//Copying Files A { at out<<inch. Rdbuf (); - out. Close (); - inch. Close (); - return 1; - } -}
Or
Linux C + + Copy file issues
Automatically include sub-directories;
Complete <[source path via command line parameters] Source file >< Destination path >
The source file name is required to support the wildcard character ' * ', for example: *.zip or *.rar, support the above two formats;
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
1 /*********************************************************************2 Features: Copying files3 Parameters: PSRC, original file name4 pDes, target file name5 return: <0, failed6 >0, Success7 8 *********************************************************************/9 #defineBuf_size 256Ten intCopyFile (Const Char* PSRC,Const Char*pDes) One { AFILE *in_file, *Out_file; - CharData[buf_size]; - size_t bytes_in, bytes_out; the LongLen =0; - if(In_file = fopen (PSRC,"RB")) ==NULL) - { - perror (PSRC); + return-2; - } + if(Out_file = fopen (PDes,"WB")) ==NULL) A { at perror (pDes); - return-3; - } - while(bytes_in = fread (data,1, Buf_size, in_file)) >0 ) - { -Bytes_out = fwrite (data,1, bytes_in, out_file); in if(Bytes_in! =bytes_out) - { toPerror ("Fatal Write error.\n"); + return-4; - } theLen + =bytes_out; *printf"copying file .....%d bytes copy\n", Len); $ }Panax Notoginseng fclose (in_file); - fclose (out_file); the return 1; + } A the /********************************************************************* + function: Copy (non-empty) directory - Parameters: PSRC, original directory name $ pDes, target directory name $ return: <0, failed - >0, Success - the *********************************************************************/ - intCopydir (Const Char* PSRC,Const Char*pDes)Wuyi { the if(NULL = = PSRC | | NULL = = pDes)return-1; - mkdir (pDes); Wu CharDir[max_path] = {0}; - CharSrcfilename[max_path] = {0}; About CharDesfilename[max_path] = {0}; $ Char*str ="\\*.*"; - strcpy (DIR,PSRC); - strcat (DIR,STR); - //First, find the files that meet the requirements in Dir A Longhfile; + _finddata_t FileInfo; the if((hfile = _findfirst (dir,&fileinfo))! =-1) - { $ Do the { the strcpy (SRCFILENAME,PSRC); thestrcat (Srcfilename,"\\"); the strcat (srcfilename,fileinfo.name); - strcpy (desfilename,pdes); instrcat (Desfilename,"\\"); the strcat (desfilename,fileinfo.name); the //Check if directory is not About //if it is not a directory, the files under the processing folder the if(! (Fileinfo.attrib &_a_subdir)) the { the CopyFile (srcfilename,desfilename); + } - Else//working with directories, recursive calls the {Bayi if(strcmp (Fileinfo.name,".") !=0&& strcmp (Fileinfo.name,"..") !=0 ) the { the copydir (srcfilename,desfilename); - } - } the} while(_findnext (hfile,&fileinfo) = =0); the _findclose (hfile); the return 1; the } - return-3; the}
View Code
Read SQLite with C + + for File replication under Windows and Linux (iii)