#include <windows.h>
#include <stdio.h>
int main(void)
{
HANDLE hFile = CreateFileA(
"data.txt", // File name
GENERIC_READ | GENERIC_WRITE, // Desired access
0, // Share mode (0 = exclusive)
NULL, // Security attributes
CREATE_ALWAYS, // Creation disposition
FILE_ATTRIBUTE_NORMAL, // Flags and attributes
NULL // Template file
);
if (hFile == INVALID_HANDLE_VALUE) {
printf("CreateFile failed. Error: %lu\n", GetLastError());
return 1;
}
const char* text = "CreateFile Function\n";
DWORD bytesWritten = 0;
BOOL ok = WriteFile(
hFile,
text,
(DWORD)strlen(text),
&bytesWritten,
NULL
);
if (!ok) {
printf("WriteFile failed. Error: %lu\n", GetLastError());
CloseHandle(hFile);
return 1;
}
printf("Wrote %lu bytes.\n", bytesWritten);
CloseHandle(hFile);
return 0;
}
/*
run:
Wrote 20 bytes.
*/