Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to append text to a file in windows?

Everytime this function is called the old text data is lost?? Tell me how to maintain previous data and appending new data.

This function is called 10 times:

void WriteEvent(LPWSTR pRenderedContent)
{
    HANDLE hFile; 
    DWORD dwBytesToWrite = ((DWORD)wcslen(pRenderedContent)*2);
    DWORD dwBytesWritten = 0;
    BOOL bErrorFlag = FALSE;

    printf("\n");

    hFile = CreateFile(L"D:\\EventsLog.txt", FILE_ALL_ACCESS, 0, NULL, OPEN_ALWAYS,   FILE_ATTRIBUTE_NORMAL, NULL);                 

    if (hFile == INVALID_HANDLE_VALUE) 
    { 
        printf("Terminal failure: Unable to open file \"EventsLog.txt\" for write.\n");
        return;
    }

    printf("Writing %d bytes to EventsLog.txt.\n", dwBytesToWrite);

    bErrorFlag = WriteFile( 
                    hFile,                // open file handle
                    pRenderedContent,      // start of data to write
                    dwBytesToWrite,  // number of bytes to write
                    &dwBytesWritten, // number of bytes that were written
                    NULL);            // no overlapped structure

    if (FALSE == bErrorFlag)
    {
        printf("Terminal failure: Unable to write to file.\n");
    }
    else
    {
        if (dwBytesWritten != dwBytesToWrite)
        {
            printf("Error: dwBytesWritten != dwBytesToWrite\n");
        }
        else
        {
            printf("Wrote %d bytes to EventsLog.txt successfully.\n",dwBytesWritten);
        }
    }

    CloseHandle(hFile);
}
like image 736
D.A.Danekhail Avatar asked Sep 21 '13 13:09

D.A.Danekhail


2 Answers

You should pass FILE_APPEND_DATA as the dwDesiredAccess to CreateFile, as documented under File Access Rights Constants (see sample code at Appending One File to Another File). While this opens the file using the correct access rights, your code is still responsible for setting the file pointer. This is necessary, because:

Each time a file is opened, the system places the file pointer at the beginning of the file, which is offset zero.

The file pointer can be set using the SetFilePointer API after opening the file:

hFile = CreateFile( L"D:\\EventsLog.txt", FILE_APPEND_DATA, 0x0, nullptr,
                    OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr );
if ( hFile == INVALID_HANDLE_VALUE ) {
    printf( "Terminal failure: Unable to open file \"EventsLog.txt\" for write.\n" );
    return;
}

// Set the file pointer to the end-of-file:
DWORD dwMoved = ::SetFilePointer( hFile, 0l, nullptr, FILE_END );
if ( dwMoved == INVALID_SET_FILE_POINTER ) {
    printf( "Terminal failure: Unable to set file pointer to end-of-file.\n" );
    return;
}

printf("Writing %d bytes to EventsLog.txt.\n", dwBytesToWrite);

bErrorFlag = WriteFile( // ...


Unrelated to your question, the calculation of dwBytesToWrite should not use magic numbers. Instead of * 2 you should probably write * sizeof(*pRenderedContent). The parameter to WriteEvent should be constant as well:
WriteEvent(LPCWSTR pRenderedContent)
like image 60
IInspectable Avatar answered Oct 07 '22 10:10

IInspectable


The parameter for appending data to a file is FILE_APPEND_DATA instead of FILE_ALL_ACCESS in the CreateFile function. Here is an example: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363778(v=vs.85).aspx

like image 43
Simon Fischer Avatar answered Oct 07 '22 10:10

Simon Fischer