如何在WIN32 API的新行中编写文件

时间:2013-03-20 02:11:58

标签: api winapi file-io newline

我正在尝试将数据写入文件。但是,我想在新行中添加新数据,但现在我不能。

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

1 个答案:

答案 0 :(得分:4)

Windows使用CR / LF组合来表示行尾,如果您希望在例如记事本中正确显示换行符,则需要编写“\ r \ n”。