The rich text file format


A rich text file is a standard format that is supposed to provide a way for word processors with different formats to exchange files. It uses escape characters to specify formatting. For example, an RTF file consisting of an unformatted letter A looks like this when displayed as text:

{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fBunch more font junk...}}
{\colortbl\red0\green0\blue0;}
\deflang1033\pard\plain\f2\fs14\b A
\par }

A real formatted file looks the same, except worse. For our purposes, the important thing is that you can’t convert an RTF file to a text file by loading it as a text file, although loading text files as RTF files is often just what you want to do when converting text files to formatted documents. The file extension doesn’t have anything to do with the format; it’s that escape \rtf1 embedded at the start of the file that indicates it’s an RTF file. The solution is to load the file as text, switch to rich text mode, modify it, and then save it as rich text. There are many ways this logic could work so that the XEditor control doesn’t guess. A client program, such as Edwina, can handle the details with error trapping or other techniques.


Loading an RTF file as a text file works, but it’s not necessarily something you want to do. The Text property will get the RTF text, and the TextRTF property will get a strange mishmash of RTF surrounded by RTF. There are other gotchas. For example, once a file has been loaded, there’s no easy way to tell whether it’s an RTF file or not. The TextRTF property will always have the magic RTF codes regardless of whether it’s an RTF file. The only way to tell is before you load. The IsRTF function in UTILITY.BAS opens a file and checks the first five characters. Of course, a randomly generated binary file just might happen to contain the magic characters, but I wouldn’t worry about it.


The FileOpen method uses IsRTF to see what kind of file you’ve really loaded. If you want to translate a file to rich text, you can change the TextMode property before saving it. It’s a good idea to use the proper extensions—TXT for text and RTF for rich text—but XEditor leaves that to you.