简体   繁体   English

文本写入文件后,mfc c ++奇怪的块字符

[英]mfc c++ strange block characters after text written to file

my program has two edit control boxes that display text from a text file, and they both have buttons associated with them that update the text files associated with them if anything is written or deleted in the edit control boxes. 我的程序有两个显示文本文件中文本的编辑控制框,并且它们都具有与之关联的按钮,如果在编辑控制框中写入或删除了任何内容,它们将更新与之关联的文本文件。 i have this code to read from a text file 我有这段代码可以从文本文件中读取

    try
{
    CStdioFile file(_T("1.txt"), CFile::modeRead);
    CString str,mainstr = _T("");
    while(file.ReadString(str))
    {

        mainstr += str;
        mainstr += _T("\r\n");
    }

    CWnd *editwindow = this->GetDlgItem(IDC_EDIT2);
    editwindow->SetWindowText(mainstr);

}
catch(CException* e)
{
    MessageBox(_T("no such file"));
    e->Delete();

}

and then this code to write to the text file 然后将此代码写入文本文件

    m_addtext.GetWindowText(m_adtxt);
if ( IsDlgButtonChecked(IDC_RADIO1) == BST_CHECKED )
{
    CStdioFile file;
    file.Open (_T("1.txt"), CFile::modeCreate | CFile::modeWrite);
    file.WriteString (m_adtxt);
    file.Close ();
}

it all works pretty much fine and dandy for what i want, but the problem is is that it adds a block character after a word if i delete a character in the edit box, and then click the update button. 一切对于我想要的东西都可以正常工作,但问题是,如果我在编辑框中删除一个字符,然后单击更新按钮,它将在单词后添加一个块字符。 sometimes it even adds a block after every word and one block on every empty line. 有时,它甚至在每个单词之后添加一个块,并在每个空行上添加一个块。 it works fine as long as it creates a new file and nothing is deleted. 只要它创建一个新文件并且什么都没有删除,它就可以正常工作。 i've tried null terminating, i've tried ccs="encoding" . 我尝试过null终止,我尝试过ccs="encoding" can anyone point me in the right direction? 谁能指出我正确的方向?

As MSDN says about CStdioFile in its default of text mode: 正如MSDN在默认文本模式下对CStdioFile 所说的那样

Text mode provides special processing for carriage return–linefeed pairs. 文本模式为回车换行符对提供特殊处理。 When you write a newline character (0x0A) to a text-mode CStdioFile object, the byte pair (0x0D, 0x0A) is sent to the file. 当您将换行符(0x0A)写入文本模式CStdioFile对象时,字节对(0x0D,0x0A)被发送到文件。 When you read, the byte pair (0x0D, 0x0A) is translated to a single 0x0A byte. 读取时,字节对(0x0D,0x0A)被转换为单个0x0A字节。

So, although your reading looks ok, because the \\n will be stripped off by the overload of ReadString that you use, then you have manually appended \\r\\n to display correctly in an edit control, however when you save the contents, every \\r\\n will be explanded to \\r\\r\\n as described in the quote above. 因此,尽管您的阅读没问题,但由于所使用的ReadString重载会剥夺\\n ,因此您手动添加了\\r\\n即可在编辑控件中正确显示,但是在保存内容时, \\r\\n将如上引言所述扩展为\\r\\r\\n

The solution is either to remove all of the \\r characters from the string before writing to CStdioFile , leaving just the \\n characters, and let CStdioFile insert the \\r for you. 解决方案是写入CStdioFile 之前从字符串中删除所有\\r字符,仅保留\\n字符,然后让CStdioFile为您插入\\r Or, much easier, just open the file in binary mode rather than text mode to suppress this conversion: 或者,更容易的是,只需以二进制模式而不是文本模式打开文件即可禁止这种转换:

CStdioFile file;
file.Open (_T("1.txt"), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
file.WriteString(m_adtxt);
file.Close ();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM