
Can’t display text in dialog box in original format
Quote:
> In a program I'm writing the user enters text in the edit window of a
> dialog box and the text is saved to a text file. Later in the program
> another dialog box displays the users input from the text file into an
> edit window. The problem is that the text is not displayed in the same
> format that it was originally in when the user typed it into the first
> dialog box.
> When the user has entered several sentences on separate lines and the
> text is displayed all the sentences run together to form one paragraph.
> It looks like the new line chars at the end of the sentences do not
> create new lines. The new line chars are displayed as black rectangles
> sandwiched between the sentences.
For historical reasons, the line separator for DOS and Windows text
files is a sequence of two characters - ASCII CR (code 13) and LF
(code 10). In standard C, the line separator for text files is a
single character, '\n', which for an environment that uses ASCII or an
ASCII-derived character set will be ASCII LF (code 10). The C library
translates between this single-character in-memory representation and
the two-character on-disk representation for you.
Windows was originally written in assembler and was not designed to
work with standard C, so edit controls were designed to be usable with
the raw data from text files, rather than C's in-memory
representation. Therefore they only recognise CR, LF as a line
separator, and display a bare LF as if it is an ordinary character.
Thus an LF displays as the font's "missing character" symbol, which is
usually a filled rectangle.
Quote:
> Also the text is word wrapping in the edit window correctly. I placed
> checks in the boxes for Vertical scroll and Auto Vscroll in the edit
> window properties of the dialog box.
> I'm using MS Developer Studio VC++ standard edition 4.0
> How can I get the text to display in the same format it was in when the
> user typed it?
If you use an edit control as a file editor, you should either (a)
read and write the files as if they are binary files ("rb" and "wb"
modes), or (b) use the Win32 file functions instead (which never
translate line separators).
--
Any opinions expressed are my own and not necessarily those of Roundpoint.