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.5/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.
In Eiffel, there is no standard set of classes for supporting files. ISE Eiffel supports the two following approaches to creating and manipulating files.
copy is local input : PLAIN_TEXT_FILE output : PLAIN_TEXT_FILE internal : ARRAY[CHARACTER] i, j : INTEGER do io.putstring ("Welcome to file-io land!%N") create input.make_open_read("aFile") create output.make_open_write("aFile.copy") create internal.make(1,1000) from input.start ; input.read_character ; i := 0 until input.off loop io.putchar(input.last_character) i := i + 1 internal.put(input.last_character,i) input.read_character end input.close io.putstring("%N%NCopy from array%N%N") from j := 0 until j = i loop j := j + 1 output.putchar(internal @ j) end output.close io.putstring("Done.") end
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. putchar(c: CHARACTER) -- Write the character c at the 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'. read_character -- Read the character at the current position. Store it in -- the attribute last_character. Advance to the next position. feature -- positioning -- deal with like a sequence start -- move to the begining of the file. off : BOOLEAN -- True if off the end of the file. end -- class PLAIN_TEXT_FILE