// 두개의 파일을 읽어서 하나의 파일로 합치는 코드
01: /* 02: * description : 두 개의 파일을 읽어서 merge 03: */ 04: 05: 06: #include <stdio.h> 07: 08: #define FILEREAD 4096 // buffer size define 09: 10: void main(void) 11: { 12: 13: FILE *fpR1, *fpR2, *fpW; 14: char buff[FILEREAD]; 15: int len; 16: 17: // first file read 18: fpR1 = fopen("C:\\test.exe","rb"); 19: if(fpR1 == NULL) 20: { 21: perror("first file read fail"); 22: return; 23: } 24: 25: // second file read 26: fpR2 = fopen("C:\\test2.exe","rb"); 27: if(fpR2 == NULL) 28: { 29: perror("second file read fail"); 30: fcloseall(); 31: return; 32: } 33: 34: // new file write 35: fpW = fopen("C:\\merged.exe","w+b"); 36: if(fpW == NULL) 37: { 38: perror("write fail"); 39: fcloseall(); 40: return; 41: } 42: 43: // get the EOF of first file and write the new file 44: while(!feof(fpR1)) 45: { 46: len = fread(buff,1,FILEREAD,fpR1); 47: 48: if(ferror(fpR1)) 49: { 50: perror("read fail first file"); 51: fcloseall(); 52: return; 53: } 54: 55: // write first file 56: if(len > 0) 57: { 58: fwrite(buff,1,len,fpW); 59: if(ferror(fpW)) 60: { 61: perror("write fail first file"); 62: fcloseall(); 63: return; 64: } 65: } 66: } 67: 68: 69: // get the EOF of second file and write the new file 70: while(!feof(fpR2)) 71: { 72: len = fread(buff,1,FILEREAD,fpR2); 73: 74: if(ferror(fpR2)) 75: { 76: perror("read fail second file"); 77: fcloseall(); 78: return; 79: } 80: 81: // write second file 82: if(len > 0) 83: { 84: fwrite(buff,1,len,fpW); 85: if(ferror(fpW)) 86: { 87: perror("write fail second file"); 88: fcloseall(); 89: return; 90: } 91: } 92: } 93: 94: fcloseall(); 95: 96: puts("file merged!"); 97: }
반응형
'Code review' 카테고리의 다른 글
[WinApi]apiStart (0) | 2009.05.27 |
---|---|
Linked List Sample (2) | 2009.05.27 |
[WinApi] 실행중인 프로세스 검사 - FindProcessName() (0) | 2009.02.01 |