2. Примеры программ работы с файлами Ввод данных
/* Программа: Ввод данных */
/* Назначение: Ввод данных с клавиатуры и */
/* запись их в файл на диске */
domains
file = datafile
dstring,cstring = string
predicates
write_message_l
check_for_ready
give_instruction
process_input_output
process_file
write_message_2
readin(dstring,cstring)
create_a_file
goal
create_a_filе.
clauses
create_a_fi1e :-
write_message_l,
process_fi1e,
write_message_2,
nl,nl,write("Press the space bar."),
readchar(_),exit.
process_fi1e :-
check_for_ready,
give_instruction,
process_input_output.
write message_l :-
nl,write("Hello"),
nl,write("This program accepts data from"),
write("keyboard and writes to a file."),nl.
check_for_ready :-
write("PLEASE PRESS SPACE BAR WHEN"),
write(" YOU ARE READY TO BEGIN."),nl,
readdevice(keyboard),
readchar(_).
give_instruction :-
nl,write("You type In data from the keyboard."),
nl,write("You terminate the Input process,"),
write(" enter 'done'. Thank you."),nl.
process_input_output :-
write("Please enter comlete filename."),
write{" Then press the return key."),nl,
write("The format of the file name is"),
write(" <file name>.<extension>."),nl,nl.
readln(Filename)f
nl,write("Start typing in . . . "),nl,
openwrite(datafile,Filename),
writedevice(datafile),
readln(Dstring),
concat(Dstring,"\13\10",Cstring),
readin(Dstring,Cstring),
closefile(datafile).
write_message_2 :-
writedevice(screen},
nl,write("Your data has been written to the file."),
write(" All done, bye!").
readin("done",_) :- !.
readin(_,Cstring) :-
write(Cstring),
readln(Dstringl),
concat(Dstringl,"\13\10",Cstringl),
writedevice(datafile), readin(Dstringl,Cstringl).
/***** конец программы *****/
/* Программа: Создание файла прямого доступа */
/* Назначение: Чтение данных с клавиатуры и */
/* запись их на диск в файл прямого доступа */
domains
file = datafile
predicates
create_a_random_access_fi1е
write_read_more(real,string)
pad_string(string,string,integer)
goal
create_a_random_access_filе.
clauses
create_a_random_access_filе :-
write("Please enter filename:"),nl,
readln(Filename),
openwrite(datafile,Filename),
closefile(datafile),
openmodify(datafile,Filename),
write("Type in data string."),nl,
readln(Dstring).
write_read_more(0,Dstring),
closefile(datafile).
write read_more(_,"done") :-
nl, write(" Press the space bar."),
readchar(_),exit.
write_read_more(Index,Dstring) :-
writedevice(datafile),
filepos(datafile, Index,0).
pad_string(Dstring,Padstring,38),
concat(Padstring,"\10\13",Cstring),
write(Cstring),
writedevice(screen),
write("Type In data string"),nl,
readln(Dstringl),
Index1 = Index + 40,
write_read_more(Indexl,Dstringl).
pad_string(Instring,Instring,Length) :-
str len(Instring,Testlength),
Testlength >= Length,!.
pad_string(Instring,Padstring,Length) :-
concat(Instring,"-",Newstring),
pad_string(Newstring,Padstring,Length).
/***** конец программы *****/