Input and Output

Eiffel provides a standard class, from the base library, called STD_FILES which provides basic input and output facilities. You can find precise details about this class in the library itself, on Ariel (see /cs/local/packages/Eiffel4.4/library/base).

You can think of STD_FILES as defining ``stream'' objects which enable streams of characters to flow between a program and the standard input and output devices, such as keyboards and screens. In this model, the output features (they all begin with put) automatically convert their argument to a string which is then passed on to the output device. Input features enable a programmer to obtain a value from the input device in a two-stage process. For example, when you invoke the local command read_line, a string of characters is read from the input device and stored in the attribute last_string. You access last_string to access the input string.

In all Eiffel programs, the object io, an instance of class STD_FILES, is automatically available to all user-defined classes. It performs the standard input and output activities, and does not need declaration or creation.

Here is an example of using io for output.

print_pay_notice is
  local
      pay: REAL
  do
      io.put_string(``Name:  '')
      io.put_string(e.name)
      io.new_line
      io.put_string(``Salary:  '')
      pay := e.gross_pay(150)
      io.put_real(pay)
  end

A typical sequence of instructions that would read in a string value from the keyboard is

io.get_string
s := io.last_string

Two other useful classes, FORMAT_INTEGER and FORMAT_DOUBLE (found in the support part of the base library) provide facilities for formatting integer and real values for output.


Files on disk

In Eiffel, there is no standard set of classes for supporting files. ISE Eiffel4.4 supports two different approaches to creating and manipulating files:

The class interface for PLAIN_TEXT_FILE presents some of the following features

class PLAIN_TEXT_FILE
...
creation
	make, make_open_read, make_open_write, make_open_append,
	make_open_read_write, make_create_read_write,
	make_open_read_append

feature -- Status report

	is_plain_text: BOOLEAN
			-- Is file reserved for text (character sequences)? (Yes)

feature -- Creating file objects

        make(fn:STRING)
			-- create file object with fn as filename
	
	make_open_write(fn:STRING)
			-- create file object with fn as filename
			-- open in write only mode
			-- create file if it does not exist

	....

feature -- Output

	put_integer, putint (i: INTEGER)
			-- Write ASCII value of `i' at current position.

	put_boolean, putbool (b: BOOLEAN) 
			-- Write ASCII value of `b' at current position.

	put_real, putreal (r: REAL) 
			-- Write ASCII value of `r' at current position.

	put_double, putdouble (d: DOUBLE) 
			-- Write ASCII value `d' at current position.

feature -- Input

	read_integer, readint 
			-- Read the ASCII representation of a new integer
			-- from file. Make result available in `last_integer'.

	read_real, readreal 
			-- Read the ASCII representation of a new real
			-- from file. Make result available in `last_real'.

	read_double, readdouble 
			-- Read the ASCII representation of a new double
			-- from file. Make result available in `last_double'.

end -- class PLAIN_TEXT_FILE