Discussions related to Visual Prolog
NuttyProloger
Posts: 10
Joined: 7 May 2024 12:02

problems with readClar and readLine from console

Post by NuttyProloger »

I am trying to write a program in Visual Prolog to delete files on my computer from a list of files in a file that will be created in Excel. I am using Prolog Version 11, build 1111, which seems to be up to date

I want the user to be able to change the directory in which the delete list is located (the files are not necessarily in this directory) and to change the name of the file containing the delete list.

I started with the example programs in PFC for Console Package. I have routines to set the current Directory and set the file name of the file with the list of files to be deleted.

I find that readChar does not accept a single character from the keyboard. It requires a press of the enter key to complete the action. But the enter key character does not get cleared, and the following readLine returns an empty string for both the Current Directory and the DeleteFileName.

I have not yet implemented any delete file functionality so my test program is not a danger of running amock.

I'd like your advice on how to fix my program.

Peter
You do not have the required permissions to view the files attached to this post.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1466
Joined: 28 Feb 2000 0:01

Re: problems with readClar and readLine from console

Post by Thomas Linder Puls »

I see the problem.

The console is a Microsoft "thing" that allows entering "commands" etc. It is made such that you can edit a line/command before it is "passed on" using the Enter key. So your program will always receive a complete line of input at the time.

readChar will however only read a single character from that line, all the remaining characters including the "Enter" (newline) character will stay in the input buffer.

I can suggest that you read the entire line instead using readLine it will read the entire line and skip the newline character.
Regards Thomas Linder Puls
PDC
Martin Meyer
VIP Member
Posts: 354
Joined: 14 Nov 2002 0:01

Re: problems with readClar and readLine from console

Post by Martin Meyer »

Class console contains procedure setModeLine/1. The procedure's description reads:
The predicate sets the state of line mode for reading of typed characters.
If #Enabled is true then read predicates return only when a carriage return character is read.
If #Enabled is false then read predicates return when one or more characters are available.
I have tested it. I think the procedure can modify the behavior of readChar/0-> in the way you want. Check the example:

Code: Select all

class console_addOn   predicates     readChar : () -> char Char.   end class console_addOn   %---   implement console_addOn   clauses     readChar() = Char :-         ModeLine = toBoolean(console::isModeLine()),         console::setModeLine(false),         Char = console::readChar(),         console::setModeLine(ModeLine).   end implement console_addOn   %======   class fileDeletion   predicates     run : ().   end class fileDeletion   %---   implement fileDeletion   class facts     workFilename : string := "".   clauses     run() :-         console::nl(),         console::write("Work file: "),         if workFilename = "" then             console::write("-not set-\n")         else             console::writeF("\"%s\"\n", workFilename)         end if,         console::write("Press 'f' key to set the work file\n"),         console::write("Press 'd' key to delete files\n"),         console::write("Press 'q' key to quit the program\n"),         console::write("Input: "),         Char = console_addOn::readChar(),         console::writeF("%c\n", Char),         if tryProcessChar(Char) then             run()         end if.   class predicates     tryProcessChar : (char Char) determ. clauses     tryProcessChar('f') :-         !,         console::write("Enter full filename of the work file: "),         workFilename := console::readLine().       tryProcessChar('d') :-         !,         console::write("Deleting not yet implemented\n"),         console::write("Press any key to continue"),         _ = console_addOn::readChar(),         console::nl().       tryProcessChar('q') :-         !,         fail.       tryProcessChar(_Char) :-         console::write("Invalid key\n"),         console::write("Press any key to continue"),         _ = console_addOn::readChar(),         console::nl().   end implement fileDeletion   %======   implement main   clauses     run() :-         console::nl(),         console::write("File Deletion\n"),         console::write("-------------\n"),         fileDeletion::run().   end implement main   %======   goal     console::runUtf8(main::run).
Regards Martin