I am trying to log actions performed by a service that I wrote using the Windows API and C language, so I created a log file system.
The problem is that with every call to CreateFile, the file is redefined, rather than just opening it and writing to the end of the file.
Here is the code of my WriteInLogfile function:
void WriteInLogFile(LPCTSTR log_string) { HANDLE hFile; DWORD dBytesWritten; if ((hFile = CreateFile(LOG_FILE_PATH, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE && (GetLastError() == ERROR_FILE_NOT_FOUND)) { if ((hFile = CreateFile(LOG_FILE_PATH, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) { if (!WriteFile(hFile, log_string, strlen(log_string), &dBytesWritten, NULL)) aff_error("WriteInLogFile"); CloseHandle(hFile); } } else { if (!WriteFile(hFile, log_string, strlen(log_string), &dBytesWritten, NULL)) aff_error("WriteInLogFile"); CloseHandle(hFile); } }
Does anyone know where this problem comes from?
Thanks;)
Billy mcnuggets
source share