We often have to write data to and read data from files. Here's an introduction.
Fist, the data writing routine. Notice the way we can do error handling by returning the error state of the function. Error handling is a useful practice, since we generally don't want programs to just crash when something goes wrong. At the very least, a good program should inform the user what it is that failed.

And here's how data is read from a file. The optional "skip" argument is there to tell the routine to skip a number of lines, containing the information string, before reading in the actual data. This is not the only way to read data in from a file, by the way; it's just an example.

The error handling introduces the "case" statement, which is a kind of extended if-then-else testing the value of one variable only.

The main body, to test the previous functions. Note that the "transpose" function is necessary to turn a matrix with 3 rows and 10 columns (made by directly combining the x, y and z arrays) into a matrix with 10 rows and 3 columns, the way data tables usually are presented.

The file produced by the program:

And the program output:

| Taner Edis | Home |
| Last modified: March 22, 2007 |