Programming/Windows&C#

[Windows/WInAPI] 윈도우 시스탬 프로그래밍 실습 02

JMob 2013. 9. 13. 16:36
728x90
반응형

 // 파일 두개를 합쳐서 하나의 파일에 복사

ctw.c

#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include <locale.h>

int _tmain (int argc, LPCTSTR argv[])
{
 HANDLE hIn1, hIn2 , hOut;
 DWORD nIn1, nIn2, nOut;
 TCHAR buffer[256];
 _tsetlocale(LC_ALL  , _T("korean"));
 if(argc != 4)
 {
  _tprintf(_T("error1"));
  return 1;
 }

 hIn1 = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 if(hIn1 == INVALID_HANDLE_VALUE)
 {
  _tprintf(_T("error2"));
  CloseHandle(hIn1);
  return 2;
 }
 hIn2 = CreateFile(argv[2], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 if(hIn2 == INVALID_HANDLE_VALUE)
 {
  _tprintf(_T("error3"));
  CloseHandle(hIn1);
  CloseHandle(hIn2);
  return 2;
 }

 hOut = CreateFile(argv[3], GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

 if(hOut == INVALID_HANDLE_VALUE)
 {
  _tprintf(_T("error4"));
  CloseHandle(hIn1);
  CloseHandle(hIn2);
  CloseHandle(hOut);
  return 3;
 }

 while(ReadFile(hIn1,buffer, 256, &nIn1,NULL)&& nIn1 >0)
 {
  WriteFile(hOut, buffer, nIn1, &nOut,NULL);
  if(nIn1 != nOut)
  {
   _tprintf(_T("error5"));
   CloseHandle(hIn1);
   CloseHandle(hOut);
   return 4;
  }

 }
 while(ReadFile(hIn2,buffer, 256, &nIn2,NULL)&& nIn2 >0)
 {
  WriteFile(hOut, buffer, nIn2, &nOut,NULL);
  if(nIn2 != nOut)
  {
   _tprintf(_T("error6"));
   CloseHandle(hIn1);
   CloseHandle(hIn2);
   CloseHandle(hOut);
   return 5;
  }

 }
 _tprintf(_T("굿\n"));
 CloseHandle(hIn1);
 CloseHandle(hIn2);
 CloseHandle(hOut);
 return 6;
}

728x90
반응형