【问题标题】:Writing new line character in file在文件中写入换行符
【发布时间】:2013-04-30 09:55:23
【问题描述】:

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

        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 structure

但我不知道为什么新行没有转储到文本文件中。

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

【问题讨论】:

  • 顺便说一句..你不是说 strcat(DataBuffer,"\n");
  • 你在文本文件中得到了什么?
  • 我怀疑问题在于您使用CreateFileWriteFile 创建和写入文件。这将避免 C 运行时库为您执行的从 '\n'"\r\n" 的自动转换。您很可能会单独获得一个换行符 '\n',这意味着一行文本后面紧跟着右下方的下一行。
  • 希望这不是您的真实代码,因为您为 100 个字符分配了空间,但看起来您正在编写大约 160 个字符。

标签: c++ string text win32gui


【解决方案1】:

在 Windows 上使用 \r\n 换行。

而且您的 XML 格式不正确。 XML 结束标记使用/ 字符,而不是\ 字符。您正在为所有 XML 值编写相同的 RegID 变量,而不是使用其他变量(mndtimeresourcetype 等)。

【讨论】:

    【解决方案2】:

    窗户? 如果是这样,请将 \n 替换为 \r\n。对于 FILE* / iostream,它是由运行时自动完成的,但不是对于 WriteFile。 当然,你需要两个行尾才能得到空行。

    顺便说一句,用strcat生成长字符串的复杂度是O(N^2),非常糟糕。

    【讨论】:

    • 这不仅仅是 O(N^2) 的复杂性使它变得糟糕。事实上,它不检查缓冲区长度,因此很容易导致缓冲区溢出。
    猜你喜欢
    • 2012-01-19
    • 2017-03-23
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多