#include <stdio.h> #include <errno.h> #define BUF_SIZE 256 int main(int argc, char *argv[]) { FILE *inFile, *outFile; char rec[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> #define BUF_SIZE 256 int main(int argc, 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> int main(int argc, 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; }
时间: 2024-10-07 20:47:04