This function writes data to a file. WriteFile starts writing data to the file at the position indicated by the file pointer.
After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written.
BOOL WriteFile( HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToWrite, DWORD * lpNumberOfBytesWritten, OVERLAPPED * lpOverlapped = NULL )
Nonzero indicates success. Zero indicates failure.
string strNewFilePath = "c:\\temp.txt";
///Create new file or open an exist file.
hFile = CreateFile( strNewFilePath,
CREATE_NEW | OPEN_EXISTING,
if (hFile == INVALID_HANDLE_VALUE)
out_str("Could not Create file."); // process error
string strWrite = "Hello World";
LPCSTR lpcszWrite = (LPCSTR)strWrite;
bool bRet = WriteFile( hFile, &lpcszWrite, strWrite.GetLength(), &dwBytesWritten,NULL);
// codes to read the file
CloseHandle(hFile);// must close file after reading