0

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". can anyone point me in the right direction?

  • just realized that while creating the file everything is fine, the problem comes when editing the file in my program, not just deleting a letter, word, or space. so basically any kind of modification of an existing file adds the strange square character to the end of the last word each line and empty lines contain 1 square. – user3179762 Jan 13 '14 at 19:00
  • Just wondering if you have enabled ES_MULTILINE for the edit control – cha Jan 13 '14 at 20:42
  • the edit control has multiline enabled. the only problem i am having, is that every time the update button is clicked, a box character is added to the last word of of every line. – user3179762 Jan 13 '14 at 20:46
  • Most likely your lines terminate with wrong line terminator sequence, e.g. "\n\r\n" instead of \r\n. Can you debug and check what's in the string buffer in the HEX form? – cha Jan 13 '14 at 21:41

1 Answers1

1

As MSDN says about CStdioFile in its default of text mode:

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. When you read, the byte pair (0x0D, 0x0A) is translated to a single 0x0A byte.

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.

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. 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 ();
Roger Rowland
  • 25,885
  • 11
  • 72
  • 113
  • the problem i have with using `CFile::typeBinary` is that reading then only displays 1 character in the edit control (in the file it's fine and does not create any boxes). well, that is unless i also read it with `CFILE::typeBinary` but then i get the same problem as before with the extra box characters. i had also tried removing `\r` characters before writing, but that didn't seem to help either. although, i think i might have to continue to work on that method just in case i didn't do it right. i'll post back if i still can't figure this out. thanks for the help. – user3179762 Jan 14 '14 at 17:39
  • @user3179762 What I meant was to *read* it as you do now but just set to binary for *writing* from the string that you have already amended to have the `\r\n` delimiters in. – Roger Rowland Jan 14 '14 at 17:57
  • when i leave reading as i do now, and then write with binary set, it then only displays one character in the edit control. – user3179762 Jan 14 '14 at 18:32
  • 1
    okay everything i have works perfectly fine if i use rich edit control 2.0, all i have to do is not add `\r\n`. i can use that instead, although i will continue to try and get it to work with edit controls. thanks again for the help. – user3179762 Jan 14 '14 at 19:09