2013-03-20 59 views
0

我試圖將數據寫入文件。但是,我想以新行添加新數據,但現在我不能。如何在WIN32中以新行寫入文件API

HANDLE hFile; 
hFile = CreateFile(_T("HELLO.txt"),    // file to open 
    GENERIC_WRITE,   // open for writing 
        0,  // share for writing 
        NULL,     // default security 
       // CREATE_NEW,   // existing file only 
       OPEN_ALWAYS, 
        FILE_ATTRIBUTE_NORMAL, // normal file 
        NULL);     // no attr. template 

// Write to File 
BOOL bErrorFlag = FALSE; 

DWORD dwPtr = SetFilePointer(hFile, 0, NULL, FILE_END); //set pointer position to end file 
LPWSTR data = _T("Data '\n'"); 
DWORD dwBytesToWrite = lstrlenW(data)*2; 
DWORD a = 0; 
bErrorFlag = WriteFile( 
       hFile,   // open file handle 
       data,  // start of data to write 
       dwBytesToWrite, // number of bytes to write 
       &dwPtr, // number of bytes that were written 
       NULL);   // no overlapped structure 
+0

*「但現在我不能」*爲什麼你不能?你在這裏遇到什麼代碼? – WhozCraig 2013-03-20 02:32:56

回答

3

Windows使用CR/LF組合來表示行的末尾,你需要寫爲「\ r \ n」,如果你想換行正確的,例如,記事本露面。

+0

非常感謝你 – MP3 2013-03-20 02:58:45