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.
EX1
int WriteFile_ex1() { string strNewFilePath = "c:\\temp.txt"; ///Create new file or open an exist file. HANDLE hFile; DWORD dwBytesWritten; hFile = CreateFile( strNewFilePath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { out_str("Could not Create file."); // process error return -1; } 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 return 1; }
CreateFile
origin.h