File Handling Lab #10 LAB #10 FILE HANDLING OBJECTIVE Write programs to implement file handling. THEORY Prolog uses a current_read_device (normally the keyboard) for reading input, and a current_write_device (normally the screen) for sending output. You can specify other devices, and can reassign the current input and output devices at run time. (This reassigning is known as redirecting I/O.) Prolog's basic file-access predicates are: openread (open a file for reading) openwrite (open a file for writing) openappend (open a file for appending) openmodify (open a file for modification) openfile (general purpose file opener) filemode (set a file to text mode or binary mode) closefile (close a file) readdevice (reassign the current_read_device or get its name) writedevice (reassign the current_write_device or get its name) To access files you use the FILE domain, which has five predefined alternatives: keyboard (for reading from the keyboard) screen (for writing to the screen) stdin (for reading from standard input) stdout (for writing to standard output) stderr (for writing to standard error) To work within and manipulate files, you use the following standard predicates: filepos (controls the position where reading or writing takes place) eof (checks whether the file position during a read operation is at the end of the file) flush (forces the contents of the internal buffer to be written to a file) existfile (verifies that a file exists) searchfile (locates a file among several directories) CSC470: Artificial Intelligence 31 File Handling deletefile (deletes a file) renamefile (renames a file) copyfile (copies a file) fileattrib (gets or sets file attributes) disk (changes the current disk and directory/subdirectory) Lab #10 Binary blocks may be transferred using: readblock (reads a binary block from a file) writeblock (writes a binary block to a file) file_bin transfers between whole files and binary blocks To search directories, the following standard predicates are available: diropen (opens a directory for searching) dirmatch (finds matching files in a directory) dirclose (closes a directory) dirfiles (non-deterministically matches files in a directory) To manipulate file names, the following are available: filenamepath (joins or splits qualified file names) filenameext (joins or splits file names and extensions) The standard predicate readterm allows your program to access facts in a file at run time. readterm can read any object written by write, plus facts describing database predicates. EXAMPLE PROGRAMS Program 1 DOMAINS file = input; output PREDICATES convert_file nondeterm repfile(FILE) run CLAUSES convert_file :- CSC470: Artificial Intelligence 32 File Handling Lab #10 repfile(input), readln(Ln), upper_lower(LnInUpper,Ln), /* converts the string to uppercase */ write(LnInUpper),nl, fail. convert_file. repfile(_). repfile(F):not(eof(F)), repfile(F). run:write("Which file do you want convert ?"), readln(InputFileName),nl, write("What is the name of the output file ?"), readln(OutputFileName),nl, openread(input, InputFileName), readdevice(input), openwrite(output, OutputFileName), writedevice(output), convert_file, closefile(input), closefile(output). GOAL run. CSC470: Artificial Intelligence 33 File Handling Lab #10 Program 2 DOMAINS file = input PREDICATES inspect_positions(file) CLAUSES inspect_positions(UserInput):readdevice(UserInput), nl,write("Position No? "), readln(X), term_str(ulong,Posn,X), readdevice(input), filepos(input,Posn,0), readchar(Y),nl, write("Char is: ",Y), inspect_positions(UserInput). GOAL write("Which file do you want to work with ?"),nl, readln(FileName), openread(input, FileName), readdevice(UserInput), inspect_positions(UserInput). EXERCISES 1. Type the above examples in Visual Prolog environment and execute them individually. 2. Explain the working of the above examples. 3. Write a Prolog program, which utilizes most of the file-related predicates, listed above. 4. Modify example 2 for handling individual lines of a text file. CSC470: Artificial Intelligence 34