#include <stdio.h>#include<errno.h>#defineBuf_size 256intMainintargcChar*argv[]) {FILE*infile, *OutFile; CharRec[buf_size]; size_t Bytesin, Bytesout; if(ARGC! =3) {printf ("usage:cpc file1 file2\n"); return 1; } inFile= fopen (argv[1],"RB"); if(InFile = =NULL) {perror (argv[1]); return 2; } outFile= fopen (argv[2],"WB"); if(OutFile = =NULL) {perror (argv[2]); return 3; } while(Bytesin = Fread (REC,1, buf_size,infile)) >0) {Bytesout= Fwrite (REC,1, Bytesin,outfile); if(Bytesin! =bytesout) {Perror ("Fatal write error."); return 4; }} fclose (InFile); Fclose (OutFile); return 0;}
#include <Windows.h>#include<stdio.h>#defineBuf_size 256intMainintargc, LPTSTR argv[]) {HANDLE hIn, hOut; DWORD NIn, Nout; CHAR Buffer[buf_size]; if(ARGC! =3) {printf ("USAGE:CPW file1 file2\n"); return 1; } hIn= CreateFile (argv[1],generic_read, File_share_read,null,open_existing,file_attribute_normal,null); if(HIn = =Invalid_handle_value) {printf ("Canno Open input file. error:%x\n", GetLastError ()); return 2; } HOut= CreateFile (argv[2],generic_write,0, Null,create_always,file_attribute_normal); if(HOut = =Invalid_handle_value) {printf ("cannot open output file. error:%x\n", GetLastError ()); return 3; } while(ReadFile (hIn, buffer, Buf_size,&nin, NULL) && nIn >0) {WriteFile (hOut, buffer, nIn,&nout,null); if(NIn! =nout) {printf ("Fatal Write error:%x\n", GetLastError ()); return 4; }} CloseHandle (HIn); CloseHandle (HOut); return 0;}
#include <Windows.h>#include<stdio.h>intMainintargc, LPTSTR argv[]) { if(ARGC! =3) {printf ("usage:cpcf file1 file2\n"); return 1; } if(! CopyFile (argv[1], argv[2],false)) {printf ("CopyFile error:%x\n", GetLastError ()); return 2; } return 0;}
File copy: Two methods of C language method, Windows API