如何在 WIN32 API 的新行中写入文件 [英] How to writefile in new line in WIN32 API

查看:33
本文介绍了如何在 WIN32 API 的新行中写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

Windows 使用 CR/LF 组合来表示行尾,如果要显示换行符,则需要写\r\n"正确地在,例如,记事本中.

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.

这篇关于如何在 WIN32 API 的新行中写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆