How to writefile in new line in WIN32 API

£可爱£侵袭症+ 提交于 2019-12-18 09:41:39

问题


I'm trying to write data to file. However, I want to add new data in new line, but now I can't.

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:


Windows uses a CR/LF combination to signify the end of line, you need to write "\r\n" if you want the line break to show up correctly in, e.g., Notepad.



来源:https://stackoverflow.com/questions/15514067/how-to-writefile-in-new-line-in-win32-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!