Previous | Table of Contents | Next |
5.2.9.2. Text Files
Text files, in Turbo Pascal, are files that may be read as text and interpreted into almost any variable, except for record and array structures and boolean types. To be interpreted into variables such as integers and reals, spaces must occur between each number in the file, with nothing else to read other than numerical data. In this sense, reading from and writing to a text file is exactly the same as reading from the keyboard, except in the cases of readln and writeln, which moves the read position in the file to the next line of text.
When experimenting with the other sources in this document, you may have observed that entering a letter when a numerical prompt was expected would crash the programs. Reading from a text file in that manner is no different because each line of text is treated like you would expect to see it treated if typed in from the keyboard. The following are some samples of the behavior of read with different variables:
Note that the current file position is always after the read variable.
The text at the particular position is translated into whatever variable is desired. A full example of reading and writing to text files is shown in Listing 5.34, as well as proper use of the functions eof() and eoln(). These functions perform status checking to determine whether the reading is at the end of the file or the end of the line of text (in that order). These functions should always be used, as you do not always know the exact description of a file that is accessed.
Listing 5.34. A demonstration of reading and writing text files.
program fig34; { demonstration of text file usage, along with eof() and eoln() } { Uses INTDATA.TXT as generated by TESTTEXT.EXE } var infile, outfile: text; total: longint; number_line, total_line: integer; objnum: integer; begin assign(infile, INTDATA.TXT); assign(outfile, DATARPT.TXT); reset(infile); rewrite(outfile); total_line := 0; while not eof(infile) do begin number_line := 0; total := 0; while not eoln(infile) do begin read(infile, objnum); inc(total, objnum); inc(number_line); end; readln(infile); inc(total_line); writeln(outfile, Line , total_line:3, : , number_line:2, numbers Average: , (total/number_line):10:4); end; close(infile); close(outfile); end.
5.2.9.3. Typed Binary Files
Another way to access a file is as a typed binary file. Binary files contain data that is stored in the file exactly as it is stored in memory. A typed binary file is a binary file in which the type of the data that is read or written is described beforehand. Therefore, the reads and writes to this type of file are always of the variable type specified (attempting to read/write any other type results in a runtime error). An example of the use of typed binary files is shown in Listing 5.35.
A typed binary file is accessed in the exact same manner as a text file with some differences. A typed binary file is described as a file of <datatype> when the file variable is written. When reset is called for a typed binary file, it may be read from as well as directly overwritten. Because this is pure binary data, readln and writeln are invalid to use with this type of file.
A procedure named seek(<binary file var>, <# of vars forward>) is used for adding random access to a binary file for purposes of reading or overwriting a position in the file, and filesize(<typed binary filevar>) returns the total number of typed variables in the file (if 38 integers are in a file, it returns 38, and not the physical size of the file on the drive). Note that eoln()s usefulness is eliminated in this file type, but eof() is still useful in the case of reading a typed binary file.
Listing 5.35. An example of typed binary file use.
program fig35; { demonstration of usage of typed binary file } { uses intdata.txt as input, outputs binary integer data } var infile: text; outfile: file of integer; int: integer; begin assign(infile, INTDATA.TXT); assign(outfile, TYPEDATA.DAT); reset(infile); rewrite(outfile); while not eof(infile) do begin while not eoln(infile) do begin read(infile, int); write(outfile, int); end; readln(infile); end; close(infile); close(outfile); end.
Previous | Table of Contents | Next |