Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to ProLog File Manipulations Prof. Dr. Nagi Soliman 2009.

Similar presentations


Presentation on theme: "Introduction to ProLog File Manipulations Prof. Dr. Nagi Soliman 2009."— Presentation transcript:

1 Introduction to ProLog File Manipulations Prof. Dr. Nagi Soliman 2009

2 File Manipulation The input-output predicates deal with the current input and output streams. By default, the current input stream is from keyboard and the current output stream is the terminal. To change the input-output streams, ProLog provides the following: see(FileName) switches the current input stream to the FileName. seeing(Name) gives Name to the current stream. seen closes current input stream and switches back to the default input stream.

3 File Manipulation tell(FileName) switches current output stream to the Filename. telling(name) gives Name to the current output stream. told closes current output stream and switches back to the default output stream.

4 File Manipulation Example: Write data to a file f1.dat: Code fw.pl: fw(File):- tell(File), write(‘student(1, nagi, 88).’),nl, told. Running fw.pl: ?- fw(‘f1.dat’). true File f1.dat: student(1, nagi, 88).

5 File Manipulation Example: Read data from a file f1.dat Code fr.pl: fr(File):- see(File), repeat, read(S), process(S), seen,!. process(end_of_file):-!. process(S):-write(S),nl,fail. Running fr.pl: ?- fr(‘f1.dat’). student(1, nagi, 88). File f1.dat: student(1, nagi 88).

6 File Manipulation Example: append 2nd data to the file f1.dat Code fa.pl: fa(File):- append(File), write(‘student(2, soliman, 45).’), nl, told. Running fa.pl: ?- fa(‘f1.dat’). true f1.dat: student(1, nagi, 88). student(2, soliman, 45).

7 File Manipulation Example: append 3rd data to the file f1.dat Code fa.pl: fa(File):- append(File), write(‘student(3, salem, 40).’), nl, told. Running fa.pl: ?- fa(‘f1.dat’). true f1.dat: student(1, nagi, 88). student(2, soliman, 45). student(3, salem, 40).

8 File Manipulation Example: append 4th data to the file f1.dat Code fa.pl: fa(File):- append(File), write(‘student(4, khalid, 90).’), nl, told. Running fa.pl: ?- fa(‘f1.dat’). true f1.dat: student(1, nagi, 88). student(2, soliman, 45). student(3, salem, 40). student(4, khalid, 90).

9 File Manipulation Example: Copy some data with condition from f1.dat to f2.dat Code fc.pl: fc:- write('enter file name to copy from: '), read(File1), write('enter file name to copy to: '), read(File2), see(File1),tell(File2),copydata,seen,told. copydata:- read(student(ID,Name,Mark)),!, (Mark>=50,!, write(passstudent(ID,Name,Mark)),write('.'),nl;true), copydata.

10 Running fc.pl: ?- fc. enter file name to copy from: ‘f1.dat’. enter file name to copy to: ‘f2.dat’. true. File f1.dat: student(1, nagi, 88). student(2, soliman, 45). File f2.dat: student(1, nagi, 88).

11 Modify fa.pl to ask the user to enter new data to append. Example: Code fanew.pl fa(File):- write('enter data to be append to the file: '), read(Data), append(File), write(Data),nl,told. Running fnew.pl: ?- fa(‘f1.dat’). enter data to be append to the file: ‘student(5, noora, 96).’. true. Run fr.pl: the last line must be: student(5, noora, 96).

12 Summary: 1- open prolog. 2- File – New – ‘fw.pl’ – ok 3- start editing the fw.pl code from slide #3 4- File – Save As – fw.pl 5- Use Prolog to consult fw.pl? 6- File – Consult – fw.pl 7- edit at the prompt ?- fw(‘f1.dat’). true 8- repeat to edit and consult fr.pl 9- run fr.pl at the prompt: ?- fr(‘f1.dat’). student(1, nagi, 99). 10- repeat the process to append using fa.pl and use fr.pl to read your new data. 11- use the same steps to create fc.pl and fcnew.pl


Download ppt "Introduction to ProLog File Manipulations Prof. Dr. Nagi Soliman 2009."

Similar presentations


Ads by Google