Files

LARD provides a reasonably complete set of file operations. Files are of two types: text files are intended for reading and writing human-readable files and binary files are for reading and writing files treated as sequences of bytes.

Text Files

Text files are accessed via handles which are variables of type tfile. Handles are associated with files using the topenin and topenout functions for input and output respectively.

Input is a line at a time. freadint and freadstr read a line of input and return its value parsed as an integer or as a string respectively. Output using the functions fprintint and fprintstr need not be line at a time; line breaks occur only when the newline escape sequence \n is sent.

The function eof indicates whether end-of-file has been reached on an input file.

To illustrate this, here is a program reads a list of numbers from one file and prints their squares to another:

	use(io).

	inp:var(tfile).
	outp:var(tfile).
	n:var(int).

	inp:=topenin("nums.dat");
	outp:=topenout("new_nums.dat");

	while (!eof(inp)) do (
	  n:=freadint(inp);
	  fprintint(outp,n*n);
	  fprintstr(outp,"\n")
	)