end_of_file FATIH UNIVERSITY Department of Computer Engineering

advertisement
Input and Output
Notes for Ch.6 of Bratko
For CENG 421 Fall03
FATIH UNIVERSITY
Department of Computer Engineering
Communication with Files
• In principle,
– program can read data from several input files (input
streams).
– Program can write data to several output files (output
streams).
– Default i/o stream called user
– Any other name can be given to the other streams
• But at any time only one input and output stream is active.(
namely current input and output stream)
FATIH UNIVERSITY
Department of Computer Engineering
Opening and Closing Files
• To change the current input stream:
– see( Filename).
– see( user).
– seen.
• To change the current output stream:
– tell( Filename).
– tell( user).
– told.
FATIH UNIVERSITY
Department of Computer Engineering
Opening and Closing Files
– see( Filename).
• Succeeds if there is nothing wrong with the file.
• Causes a side effect: The current input stream is
changed to Filename.
– Typical operations for reading from a file:
• see( Filename).
• read_from_file(Info).
• see(user).
FATIH UNIVERSITY
Department of Computer Engineering
Opening and Closing Files
– tell( Filename).
• Succeeds if there is nothing wrong with the file.
• Causes a side effect: The current output stream is
changed to Filename.
– Typical operations for writing to a file:
• tell( Filename).
• write_to_file(Info).
• tell(user).
FATIH UNIVERSITY
Department of Computer Engineering
Opening and Closing Files
• seen
– Closes the current input file
• told
– Closes the current output file
• Sequential access is the most frequently used one
• Some implementations use Random access.
FATIH UNIVERSITY
Department of Computer Engineering
Sequential Access
• Sequential files behaves like terminals
• Request for reading
– read from the current position in the current
input stream
– Current position will be moved to the next
unread item.
– Request for reading at the end of the file
returns the atom end_of_file
FATIH UNIVERSITY
Department of Computer Engineering
Sequential Access
• Request for writing
– Append to the end of the current output stream
– Not possible to go backward and to overwrite
part of a file.
FATIH UNIVERSITY
Department of Computer Engineering
Files in Prolog
• Two ways in which the files can be viewed
– Consider characters as the basic elements
• Read/write one character at a time
• Use get, get0, put built-in predicates.
– Consider bigger units (or terms in Prolog) of
information as the building blocks of the file.
• Read/write one term at a time
• Use read, write built-in predicates.
FATIH UNIVERSITY
Department of Computer Engineering
Reading and Writing Terms
• read( X).
– reads a term from the current input stream
– deterministic: no backtracking on failure
– X = end_of_file when at eof
– Each term in the input file must be followed by a full stop and a
space or a carriage return.
• write( X).
– displays term X
– Knows how to display all terms
• Additional built-in predicates for formatting:
– tab(N):put N spaces
– nl: new line
– Examples of the book on pages 147-154.
FATIH UNIVERSITY
Department of Computer Engineering
Manipulating Characters
• put( C): A Character is written.
– C is an ASCII code
• get0( C): A single char is read.
– C is instantiated to an ASCII code
• get( C): A single char is read.
– Like get0( C), but skips blanks(non-printable
chars).
• Example of the book on page 155
FATIH UNIVERSITY
Department of Computer Engineering
Constructing and Decomposing
Atoms
• name/2
– Name( Atom, L) if L is the list of ASCII codes of the
characters that make up Atom
– name is bi-directional
– name(zx232,[122,120,50,51,50]
– Example: Prefix test:
• Atoms=order1, order2, driver1,driver2, taxia1, taxilux
• taxi(X) tests whether X represents a taxi
taxi(X) :-
FATIH UNIVERSITY
name(X,XL),
name(taxi,TL),
conc(TL,_,XL).
Department of Computer Engineering
Example
% Figure 6.2 A procedure to transform a sentence into a list of atoms.
/*Procedure getsentence reads in a sentence and combines the words
into a list of atoms. For example
getsentence( Wordlist)
produces
Wordlist = [ 'Mary', was, pleased, to, see, the, robot, fail]
if the input sentence is:
Mary was pleased to see the robot fail.
*/
FATIH UNIVERSITY
Department of Computer Engineering
getsentence( Wordlist) :get0( Char),
getrest( Char, Wordlist).
getrest( 46, [] ) :- !.
% End of sentence: 46 = ASCII for '.'
getrest( 32, Wordlist) :- !,
getsentence( Wordlist).
% 32 = ASCII for blank
% Skip the blank
getrest( Letter, [Word | Wordlist] ) :getletters( Letter, Letters, Nextchar), % Read letters of current word
name( Word, Letters),
getrest( Nextchar, Wordlist).
getletters( 46, [], 46) :- !.
% End of word: 46 = full stop
getletters( 32, [], 32) :- !.
% End of word: 32 = blank
getletters( Let, [Let | Letters], Nextchar) :get0( Char),
getletters( Char, Letters, Nextchar).
FATIH UNIVERSITY
Department of Computer Engineering
Download