Previous topic | Ada Home Page | Index

File of lines of characters

When you need to process every character in a file, and you are also concerned about line boundaries, your code will always fit the following pattern:

Note the loop structure:

Using the template

You can use the above template to write your file processing code:

  1. Decide what (if anything) needs to be done before any of the file is read.
  2. Decide what (if anything) needs to be done before you start to read a new line.
  3. Decide what needs to be done to process a character that has been read in.
  4. Decide what final processing needs to be done (if anything) when the end of a line is reached.
  5. Decide what final processing needs to be done (if anything) once the end of data is reached.

Examples

The previous example program which copies one file to another runs a small risk. It reads a whole line at a time, assuming the maximum length of any line is 200 characters. Any line that is longer than 200 characters will be truncated.

That problem can be overcome by copying each character individually. An example shows how.

Another example program tabulates the frequency of occurrence of each different letter in a file of text.


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 23 Feb 96