Last active
January 10, 2019 22:08
-
-
Save vkocjancic/80c637d8a9cab30383a71f17f6c1bdc8 to your computer and use it in GitHub Desktop.
A simple sample for async write to file in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pch.h" | |
#include <iostream> | |
#include <Windows.h> | |
#include <atlstr.h> | |
VOID WINAPI FileWrittenCallback(DWORD dwErrorCode, DWORD dwBytesTransferred, LPOVERLAPPED lpOverlapped) { | |
if (dwErrorCode != 0) { | |
fprintf(stdout, "CompletionRoutine: Unable to write to file! Error: %u, AddrOverlapped: %p\n", dwErrorCode, lpOverlapped); | |
} | |
else { | |
fprintf(stdout, "CompletionRoutine: Transferred: %u Bytes, AddrOverlapped: %p\n", dwBytesTransferred, lpOverlapped); | |
} | |
} | |
int main() | |
{ | |
HANDLE hFile = CreateFile(_T("K:\\foo.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); | |
if (INVALID_HANDLE_VALUE == hFile) { | |
fprintf(stdout, "Could not create file! Error %u\n", GetLastError()); | |
return -1; | |
} | |
OVERLAPPED oFile; | |
oFile.Offset = 0xFFFFFFFF; | |
oFile.OffsetHigh = 0xFFFFFFFF; | |
CString text = _T("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy\r\n"); | |
if (!WriteFileEx(hFile, text, text.GetLength() * 2, &oFile, (LPOVERLAPPED_COMPLETION_ROUTINE)FileWrittenCallback)) { | |
fprintf(stdout, "Unable to write to file! Error %u\n", GetLastError()); | |
} | |
CloseHandle(hFile); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment