如何将文本附加到windows中的文件?

时间:2022-11-05 13:57:42

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:

这个函数被称为10倍:

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);
}

2 个解决方案

#1


8  

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

将数据附加到文件的参数是FILE_APPEND_DATA,而不是CreateFile函数中的FILE_ALL_ACCESS。这里有一个例子:http://msdn.microsoft.com/en-us/library/windows/desktop/aa363778(v=vs.85).aspx

#2


6  

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:

您应该将FILE_APPEND_DATA作为dwDesiredAccess传递给CreateFile,如文件访问权限常量(请参阅将一个文件附加到另一个文件的示例代码)所示。当使用正确的访问权限打开文件时,您的代码仍然负责设置文件指针。这是必要的,因为:

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:

在打开文件后,可以使用SetFilePointer API设置文件指针:

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)

#1


8  

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

将数据附加到文件的参数是FILE_APPEND_DATA,而不是CreateFile函数中的FILE_ALL_ACCESS。这里有一个例子:http://msdn.microsoft.com/en-us/library/windows/desktop/aa363778(v=vs.85).aspx

#2


6  

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:

您应该将FILE_APPEND_DATA作为dwDesiredAccess传递给CreateFile,如文件访问权限常量(请参阅将一个文件附加到另一个文件的示例代码)所示。当使用正确的访问权限打开文件时,您的代码仍然负责设置文件指针。这是必要的,因为:

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:

在打开文件后,可以使用SetFilePointer API设置文件指针:

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)