Chapter 5. Text files

Table of Contents
5.1. How do text files work?
5.2. Writing text data to a stream file
5.3. Reading text data from a stream file
5.4. Example of writing and reading text files
5.5. Using code pages with text files
5.6. Example of writing & creating an ASCII text file
5.7. Example of a report in ASCII

5.1. How do text files work?

As I mentioned at the start of this eBook, a stream file is simply a long string of bytes. How that string of bytes is used is up to the software that reads and writes the file.

One of the most common ways of organizing the data in a stream file is called a "text file." Text files are made up of human-readable text organized into variable-length records called "lines." Each line is intended to be printed on a single row of a display or a page of paper.

Text files are important to use because they are so widely used. This list is just a sample of the many places text files are used:

In order to make it easy to store variable-length lines, a special character is written to signify the end of a line. This character is called, appropriately, "end-of-line character" or more commonly the "new line character."

Unfortunately, not everyone agrees on what character should be used as the new line character. On Unix systems, they use the "LF" (line feed) character. On Macintosh systems, the "CR" (carriage return) character is used to indicate a new line, and for Microsoft Windows, they use the two characters in combination, CR followed by LF.

In our examples, we will use the Windows convention of "CRLF" since it is the most widely used. Changing your code to use just CR or just LF is easy enough to do if you need to work with one of the other formats.