在文件中写入新行字符 [英] Writing new line character in file

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

问题描述

我只想在文件中写入空白行。我使用以下代码但不工作。

I just want write blank line into the file. i use following code but is not working.

char* RegID;
        RegID = "10";
        char* mndtime;
        mndtime  = "10";
        char* resourcetype;
        resourcetype  = "Backup";
        char* ressubtype;
        ressubtype = "shadowprotect";
        char* DataBuffer = new char[100];

        StrCpy(DataBuffer,"<wpshadowprotectstatus>");
        strcat(DataBuffer,"\n");
        strcat(DataBuffer,"<mndtime>");
        strcat(DataBuffer,RegID);
        strcat(DataBuffer,"<\\mndtime>\n");
        strcat(DataBuffer,"<resourcetype>");
        strcat(DataBuffer,RegID);
        strcat(DataBuffer,"<\\resourcetype>\n");
        strcat(DataBuffer,"<ressubtype>");
        strcat(DataBuffer,RegID);
        strcat(DataBuffer,"<\\ressubtype>\n");
        strcat(DataBuffer,"<jobname>");
        strcat(DataBuffer,RegID);
        strcat(DataBuffer,"<\\jobname>\n");
        strcat(DataBuffer,"<jobstarttime>");
        strcat(DataBuffer,RegID);
        strcat(DataBuffer,"<\\jobstarttime>\n");
        HANDLE hFile;

        hFile = CreateFile("text.txt",                // name of the write
                           GENERIC_WRITE,          // open for writing
                           0,                      // do not share
                           NULL,                   // default security
                           CREATE_NEW,             // create new file only
                           FILE_ATTRIBUTE_NORMAL,  // normal file
                           NULL);                  // no attr. template

        if (hFile == INVALID_HANDLE_VALUE)
        {
            return 0;
        }
        DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
        DWORD dwBytesWritten = 0;
        BOOL bErrorFlag = FALSE;
        bErrorFlag = WriteFile(hFile,           // open file handle
                        DataBuffer,      // start of data to write
                        dwBytesToWrite,  // number of bytes to write
                        &dwBytesWritten, // number of bytes that were written
                        NULL);            // no overlapped 

结构

但是我不知道为什么新行不在文本文件中转储。



注意: - 1)我不想使用std :: library c ++。 2)不想使用xml解析器。

structure
but i dont known why new line is not dump in text file.

Note :- 1)I dont want to use std:: library c++. 2)Dont want to use xml parser.

推荐答案

问题的一个原因可能是您以二进制模式写入文件。如果您使用std c库并以文本模式(w)打开文件,则换行符(''\ n'')将自动转换为平台相关的换行符(Windows上的\\\\ n) )。如果以二进制模式写入文件,则只写入''\ n''。一些编辑/查看器程序接受换行符(windows和unix)(记事本++),但有些不(例如windows记事本)。您可以尝试通过写出\\\\ n而不是\ n来解决问题。
One source of the problem can be that you are writing to the file in binary mode. If you use the std c library and open the file in text mode ("w") then the newline characters (''\n'') are automatically converted to platform dependent newlines ("\r\n" on windows). If you write to a file in binary mode then only the ''\n'' is written. Some editor/viewer programs accept both newlines (windows and unix) (notepad++) but some don''t (for example windows notepad). You could try to fix your problems by writing out "\r\n" instead of "\n".


新行几乎肯定会被写入文件,但是我怀疑编辑并不认为他们是全线突破。您是否有机会使用记事本?



如果您附加\\\\ n而不是\ n,那么它应该显示为一行在记事本中休息。



然而.....



1.你知道你有一个DataBuffer中的缓冲区溢出,您写入的文本数量显然超过100个字符。这可能会导致一些奇怪的行为。



2.如果您正在写出XML数据,那么结束标记应该是前向而不是反斜杠,并且您不会关闭wpshadowprotectstatus标签。



3.我认为你是在删除缓冲区并在其生命周期结束时关闭HFILE手柄?



您还应该将const字符串声明为:



The new lines are almost certainly being written to the file, but I suspect the editor doesn''t recognise them as a full line break. Are you using notepad by any chance?

If you append "\r\n" instead of "\n" then it should be visible as a line break in notepad.

However .....

1. You do know you have a buffer overrun in DataBuffer, the amount of text you are writing to is clearly over 100 characters. This could result in some odd behaviour.

2. If you are writing out XML data then the closing tags should be forward not backslash, and you do not close the wpshadowprotectstatus tag.

3. I presume you are deleting the buffer and closing the HFILE handle at the end of it''s lifetime?

You should also probably declare the const strings as:

const char* RegID = "10";
const char* mndtime  = "10";
const char* resourcetype  = "Backup";
const char* ressubtype = "shadowprotect";


使用XML编写器可以使这更简单。 XmlLite [ ^ ]是一个很好的基本版,可以在C ++中使用。
You could make this so much simpler by using an XML writer. XmlLite[^] is a good basic one that you can use in C++.


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

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