如何在WIN32 API新行WRITEFILE [英] How to writefile in new line in WIN32 API

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

问题描述

我尝试将数据写入到文件。不过,我想补充的新行的新数据,但现在我不能。

  HANDLE HFILE;
HFILE =的CreateFile(_T(hello.txt的),//文件打开
    GENERIC_WRITE,//打开写作
                   0,//份额写作
                   NULL,//默认安全
                 只有// CREATE_NEW,//现有的文件
                 OPEN_ALWAYS,
                   FILE_ATTRIBUTE_NORMAL,//正常的文件
                   空值); //没有ATTR。模板//写入文件
BOOL bErrorFlag = FALSE;DWORD dwPtr = SetFilePointer(HFILE,0,NULL,FILE_END); //设置指针的位置到文件结尾
LPWSTR数据= _T(数据的'\\ n');
DWORD dwBytesToWrite = lstrlenW(数据)* 2;
DWORD一个= 0;
bErrorFlag = WriteFile的(
                HFILE,//打开文件句柄
                数据//启动要写入的数据
                dwBytesToWrite,//字节数写
                &安培; dwPtr,//字节数写的
                空值); //没有重叠结构


解决方案

Windows使用CR / LF组合来表示行的末尾,你需要写为\\ r \\ n,如果你想换行现身正确,如记事本。

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 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新行WRITEFILE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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