How to create a file in TXT format from Delphi / C++Builder / Lazarus
The TXT file stores text documents with information organized in the form of lines. In modern systems, strings are separated by line separators; in the past, storing strings in the form of records of constant or variable length was used. Sometimes the end of a text file (especially if the file size information is not stored in the file system) is also marked with one or more special characters known as end-of-file markers. It is important to note that a text file can contain both formatted and unformatted text (sometimes the letter spacing and font boldness / italicization properties are regulated by special esc-sequences – control characters).
Today we are used to the fact that font formatting, the elements of the style, certain sizes according to user preferences can be applied for any symbol. There are more specialized formats like XML, PHP, CSV and CHM; the TXT format is laid in their structure as a basis.
You can open a TXT file not only within a computer, but also on a variety of mobile phones, smartphones, tablets and special readers, so you can work with Text File on different versions of Microsoft Windows, Mac OS, Linux, iOS, Android and Windows Mobile platforms.
Most users of the Microsoft system use a regular Notepad or the popular Microsoft Office software package in order to open TXT and users of the Mac OS use the built-in TextEditor.
Why might we need just the plain text? For example, powerful high-performance printing devices in banks still print plain text (yes, formatted – but text). And from the point of view of information storage compactness without additional compression, there is nothing like it. Corporate sector loves “plain TXT”.
Creating TEXT in Delphi without a single line of code!
First of all, compile the project and implement FastReport with the ‘export to Text file’ component. Generate a report, launch it and click on ‘Save’ in the preview window (below I will describe how to save in TXT format using a Delphi code). The window with export to RTF settings will appear.
We can choose which pages of our document to export, certain pages or a range.
Export settings – whether to export the page breaks, create frames, add empty lines (after each line of the report), use OEM codepage (Windows encoding is set as default).
As usual, you can specify where to save your TXT file (in the local storage, send to E-mail, upload to FTP or cloud).
Open after export – the resulting file will be opened immediately after export by any software associated with TXT documents (Notepad, TextEditor).
Code for saving in TXT format directly from Delphi / Lazarus
Now we can save complex documents in a regular TXT format file! By the way, this is not the only way to create complex but entirely text documents. Please note that when you select File – New… (with ellipsis) in the FastReport report designer a window for choosing what exactly you want to create will appear.
Select “Dot-Matrix Report” and go into the TXT-report creation mode. But we will talk more about this another time.