2017-04-20 137 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); 
}