본문 바로가기

old_Coding/C/C++

[C] File I/O

#include <windows.h>
#include <iostream>
#include <stdio.h>

// 형식 : 이영득_2주차_파일예제.zip


const int MAX_FILENAME_SIZE = 20 ;
const int MAX_BUFFER_SIZE = 256 ;


int main(void)
{
 TCHAR FileName[MAX_FILENAME_SIZE] ;
 TCHAR InputBuffer[MAX_BUFFER_SIZE] ;

 ::ZeroMemory(FileName, MAX_FILENAME_SIZE* sizeof(TCHAR) ) ;
 ::ZeroMemory(InputBuffer, MAX_BUFFER_SIZE* sizeof(TCHAR) ) ;


 std::cout << "Input File Name: " ;
 wscanf(L"%s", FileName) ;
 std::cout << std::endl ;

 HANDLE hTemp = ::CreateFile(FileName, GENERIC_ALL, 0, NULL, CREATE_NEW, 0, NULL) ;

 std::cout << "Input a Sentence to write in the file: " << std::endl ;
 wscanf(L"%s", InputBuffer) ;
 std::cout << std::endl ;

 DWORD nSize = wcslen(InputBuffer) ;
 DWORD nResult = 0 ;

 ::WriteFile (hTemp, InputBuffer, nSize*sizeof(TCHAR), &nResult, NULL) ;
 if (nSize != nResult*sizeof(TCHAR))
  std::cout << "Error: nSize != nResult" << std::endl ;
 

 // the file will be opend, and printf inside the file
 int i ;
 std::cin >> i ;

 return 0 ;
}

 

 

 

 

///////////////////

 

#include <windows.h>
#include <iostream>
#include <stdio.h>

#include <locale.h>

// 형식 : 이영득_2주차_파일예제.zip


const int MAX_FILENAME_SIZE = 20 ;
const int MAX_BUFFER_SIZE = 256 ;


int main(void)
{
 _wseflocale(LC_ALL, L"korean") ;
 TCHAR FileName[MAX_FILENAME_SIZE] ;
 TCHAR InputBuffer[MAX_BUFFER_SIZE] ;

 ::ZeroMemory(FileName, MAX_FILENAME_SIZE* sizeof(TCHAR) ) ;
 ::ZeroMemory(InputBuffer, MAX_BUFFER_SIZE* sizeof(TCHAR) ) ;


 std::cout << "Input File Name: " ;
 wscanf(L"%s", FileName) ;
 std::cout << std::endl ;

 HANDLE hTemp = ::CreateFile(FileName, GENERIC_ALL, 0, NULL, CREATE_NEW, 0, NULL) ;

 std::cout << "Input a Sentence to write in the file: " << std::endl ;
 wscanf(L"%s", InputBuffer) ;
 std::cout << std::endl ;

 DWORD nSize = wcslen(InputBuffer) ;
 DWORD nResult = 0 ;

 ::WriteFile (hTemp, InputBuffer, nSize*sizeof(TCHAR), &nResult, NULL) ;
 if (nSize != nResult*sizeof(TCHAR))
  std::cout << "Error: nSize != nResult" << std::endl ;
 

 // the file will be opend, and printf inside the file

 TCHAR ReadBuffer[MAX_BUFFER_SIZE] ;
 ::ZeroMemory(ReadBuffer, MAX_BUFFER_SIZE*sizeof(TCHAR)) ;

 nSize = nSize*2 ;
 ::ReadFile(hTemp, ReadBuffer, MAX_BUFFER_SIZE*sizeof(TCHAR), &nSize, NULL) ;
 if ( nOut < 0 )
  std::cout << "Error: nResult<0" << std::endl ;

 
 wprintf(L"the sentence inside the file: \n %s", ReadBuffer) ;


 int i ;
 std::cin >> i ;

 ::CloseHandle(hTemp) ;

 return 0 ;
}