티스토리 뷰

프로그래밍

winapi CreateFile 예제

worniworni 2022. 1. 27. 00:32
반응형
#include <Windows.h>
#include <string>

int main()
{
   // Open a handle to the file
   HANDLE hFile = CreateFile(
      L"C:\\NewFile.txt",     // Filename
      GENERIC_WRITE,          // Desired access
      FILE_SHARE_READ,        // Share mode
      NULL,                   // Security attributes
      CREATE_NEW,             // Creates a new file, only if it doesn't already exist
      FILE_ATTRIBUTE_NORMAL,  // Flags and attributes
      NULL);                  // Template file handle

   if (hFile == INVALID_HANDLE_VALUE)
   {
      // Failed to open/create file
      return 2;
   }

   // Write data to the file
   std::string strText = "Hello World!"; // For C use LPSTR (char*) or LPWSTR (wchar_t*)
   DWORD bytesWritten;
   WriteFile(
      hFile,            // Handle to the file
      strText.c_str(),  // Buffer to write
      strText.size(),   // Buffer size
      &bytesWritten,    // Bytes written
      nullptr);         // Overlapped

   // Close the handle once we don't need it.
   CloseHandle(hFile);
}
반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함