Discussions related to Visual Prolog
ahmednadi
Active Member
Posts: 37
Joined: 15 Sep 2009 14:06

Append records to a file.

Post by ahmednadi »

Dear Sir;
Could you help me to append records form an internal DB to an external file?
Regards;
AHMED
User avatar
drspro2
VIP Member
Posts: 105
Joined: 28 Apr 2006 12:03

Post by drspro2 »

hi , the predicate save saves the whole internal databse to a text file,
ahmednadi
Active Member
Posts: 37
Joined: 15 Sep 2009 14:06

Post 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
User avatar
Thomas Linder Puls
VIP Member
Posts: 1466
Joined: 28 Feb 2000 0:01

Post 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.
Regards Thomas Linder Puls
PDC
User avatar
drspro2
VIP Member
Posts: 105
Joined: 28 Apr 2006 12:03

Post 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()),
ahmednadi
Active Member
Posts: 37
Joined: 15 Sep 2009 14:06

Post by ahmednadi »

Dear Sir;
Thank you.
The order of the facts is important.
Could you help me?
Regards;
AHMED
User avatar
Thomas Linder Puls
VIP Member
Posts: 1466
Joined: 28 Feb 2000 0:01

Post 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().
Regards Thomas Linder Puls
PDC