Page 1 of 1

Append records to a file.

Posted: 2 Apr 2015 9:01
by ahmednadi
Dear Sir;
Could you help me to append records form an internal DB to an external file?
Regards;
AHMED

Posted: 2 Apr 2015 9:20
by drspro2
hi , the predicate save saves the whole internal databse to a text file,

Posted: 2 Apr 2015 10:32
by ahmednadi
Dear Sir;
Thank you.
I want to append the whole internal DB. there is an file contents some records and we want to add new records to them.
Regards;
AHMED

Posted: 2 Apr 2015 12:04
by Thomas Linder Puls
You will need to consult the contents of the file and then save everything.

If the order of the facts are important you will need to do something more complex.

Posted: 2 Apr 2015 12:12
by drspro2
hi maybe something like this, depends on which version of prolog you use,

Code: Select all

append_to_file():-  Fn2 = "dbtextfile.txt"   file5x::closefile(fileSelector::gg()),     trap(file5x::openappend(fileSelector::gg(),Fn2),_,fail),   file5x::writedevice(fileSelector::gg()),    record(A,B),     file5x::write(A2,"\n"),   fail.   append_to_file():-   file5x::closefile(fileSelector::gg()),   file5x::writedevice(fileSelector::stdout()),

Posted: 2 Apr 2015 12:42
by ahmednadi
Dear Sir;
Thank you.
The order of the facts is important.
Could you help me?
Regards;
AHMED

Posted: 2 Apr 2015 19:46
by Thomas Linder Puls
You should not (= absolutely not at all) use file5x, it is only intended to be used when migrating old Vip5.2 code.

The code corresponds to this "up-to-date" code:

Code: Select all

    append_to_file() :-         Fn2 = "dbtextfile.txt",         OS = outputStream_file::append(Fn2),         foreach record(A, B) do             OSwrite(A2, "\n")         end foreach,         OS:close().
The code still contains the same bugs ;-) (variable A2 is unbound).

If you want to try something like this you will have to write all facts, and also their functors (and a terminating dot):

Code: Select all

    append_to_file() :-         Fn2 = "dbtextfile.txt",         OS = outputStream_file::append(Fn2),         foreach record(A, B) do             OS:write("%.\n", record(A, B))         end foreach,         foreach anotherFact(A, B, C, D) do             OS:write("%.\n", anotherFact(A, B, C, D))         end foreach,         % continue for all facts         OS:close().