2017-04-20 79 views
0

我已经搜索过要做到这一点,但我无法找到我做错了什么。我试图让这个函数在每次调用时附加数据,但它总是这样做一旦。如果该文件不存在,它会创建一个新的和文件编写一次,如果该文件存在,它什么都不做(或者覆盖)如何在win32中追加文件中的数据

void WriteToFile (char data[],wchar_t filename[]) 
{ 
    HANDLE hFile; 
    DWORD dwBytesToWrite = (DWORD)strlen(data); 
    DWORD dwBytesWritten ; 
    BOOL bErrorFlag = FALSE; 


    hFile = CreateFile((LPCWSTR)filename,   // name of the write 
     GENERIC_WRITE,   // open for writing 
     0,      // do not share 
     NULL,     // default security 
     CREATE_NEW,    // create new file only 
     FILE_ATTRIBUTE_NORMAL, // normal file 
     NULL);     // no attr. template 

    if (hFile == INVALID_HANDLE_VALUE) 
    { 
     DisplayError(TEXT("CreateFile")); 
     _tprintf(TEXT("Terminal failure: Unable to open file \"%s\" for write.\n"), filename); 
     return; 
    } 


    bErrorFlag = WriteFile(
    hFile,    // open file handle 
    data,    // 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) 
    { 
     DisplayError(TEXT("WriteFile")); 
     printf("Terminal failure: Unable to write to file.\n"); 
    } 
    else 
    { 
     if (dwBytesWritten != dwBytesToWrite) 
     { 
     // This is an error because a synchronous write that results in 
     // success (WriteFile returns TRUE) should write all data as 
     // requested. This would not necessarily be the case for 
     // asynchronous writes. 
     printf("Error: dwBytesWritten != dwBytesToWrite\n"); 
     } 
     else 
     { 
     _tprintf(TEXT("Wrote %d bytes to %s successfully.\n"), dwBytesWritten, filename); 
    } 
} 

CloseHandle(hFile); 
} 

而这正是我所说的功能WM_COMMAND

//When a menu item selected execute this code 
case IDM_FILE_SAVE: 
     saveBool = true; 
     char Str[] = "this is my own data"; 
     wchar_t filename[] = L"data.txt"; 
     WriteToFile(Str, filename); 
     break; 
+1

为什么不使用C++标准库? –

+2

您需要在* dwCreationDisposition *中使用* OPEN_ALWAYS *而不是* CREATE_NEW *。并在* dwDesiredAccess *必须* FILE_APPEND_DATA *,但不* GENERIC_WRITE * – RbMm

+0

@ RBMm谢谢,这很好运行 –

回答

4

如果该文件存在,它什么都不做

因为它应该是。每CreateFile()文档:

CREATE_NEW
创建一个新文件,只有当它不存在

如果指定的文件存在,则该函数将失败,并且最后一个错误代码将设置为ERROR_FILE_EXISTS(80)

如果指定的文件不存在并且是可写位置的有效路径,则会创建一个新文件。

对于你正在尝试做的,使用OPEN_ALWAYS代替:

OPEN_ALWAYS
打开一个文件,始终。

如果指定的文件存在,则函数成功并且最后的错误代码被设置为ERROR_ALREADY_EXISTS(183)。

如果指定的文件不存在并且是可写位置的有效路径,则该函数将创建一个文件,并将最后一个错误代码设置为零。

可以使用FILE_APPEND_DATA访问符有CreateFile()自动寻找到文件末尾创建后/打开它(否则,您必须寻求使用SetFilePointer/Ex()手动),然后再写入新的数据文件。

试试这个:

void WriteToFile (char *data, wchar_t *filename) 
{ 
    HANDLE hFile; 
    DWORD dwBytesToWrite = strlen(data); 
    DWORD dwBytesWritten ; 
    BOOL bErrorFlag = FALSE; 

    hFile = CreateFileW(filename, // name of the write 
     FILE_APPEND_DATA,   // open for appending 
     FILE_SHARE_READ,   // share for reading only 
     NULL,      // default security 
     OPEN_ALWAYS,    // open existing file or create new file 
     FILE_ATTRIBUTE_NORMAL,  // normal file 
     NULL);      // no attr. template 

    if (hFile == INVALID_HANDLE_VALUE) 
    { 
     DisplayError(TEXT("CreateFile")); 
     wprintf(L"Terminal failure: Unable to create/open file \"%s\" for writing.\n", filename); 
     return; 
    } 

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

     if (!bErrorFlag) 
     { 
      DisplayError(TEXT("WriteFile")); 
      printf("Terminal failure: Unable to write to file.\n"); 
      break; 
     } 

     wprintf(L"Wrote %u bytes to \"%s\" successfully.\n", dwBytesWritten, filename); 

     data += dwBytesWritten; 
     dwBytesToWrite -= dwBytesWritten; 
    } 

    CloseHandle(hFile); 
}