reads one newline-terminated line from a file or pipe

Parameters

 file - file descriptor or file name

Description

 • The readline function attempts to read the next line from the specified file.
 • The readline function returns a string consisting of the line read from the file. The newline character at the end of the line is removed before returning the string.
 • If there are no more lines left to read, readline returns 0 instead of a string to indicate that the end of the file has been reached. If the file was specified as a file name, it is automatically closed at this point.
 • The call readline(terminal) reads a line from the top level input stream. If Maple is being run interactively, any call to readline(terminal) will read a line from the user, even if a file read is in effect. If Maple is being run in batch mode (for example, using < for command line redirection) then readline(terminal) reads from the batch input file, even if a file read is in effect, since there is no user.
 • If a file name is given, and that file is not yet open, it is opened in READ mode with type TEXT.
 • The call readline(-1) is similar to readline(default), except that the Maple input preprocessor is invoked. Lines beginning with "!" are executed as operating system commands and lines beginning with  "?" are translated as calls to the help function.  Also, the readline(-1) command reads from the next prompt, while the readline(default) command opens a dialog for input.

 • Parallel calls to file i/o commands on the same file descriptor will be serialized in an arbitrary order.  If you need the commands to execute in a particular order you must use Maple's synchronization tools to enforce this.  See Threads:-Mutex.

Examples

Get only the first and last lines of a file called "infile.text". NOTE: This example will only work if you have the "infile.text" file.

 > $\mathrm{first}≔\mathrm{readline}\left("infile.text"\right)$
 > $\mathrm{line}≔\mathrm{readline}\left("infile.text"\right)$
 > $\mathbf{while}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{line}\ne 0\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{last}≔\mathrm{line};\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{line}≔\mathrm{readline}\left("infile.text"\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}$

In the next example, the readline command is used to get input from the keyboard for a simple question: Guess a randomly generated integer between values 1 to 5.

 > GetNumber := proc()    local number;    randomize();    number := rand( 1 .. 5 )();    printf( "Guess a number between 1 and 5:\n>> " );    while( parse( readline() ) <> number ) do        printf( "Try again!\n>> " );    end do:   printf( "Correct! The number was %d.\n", number ); end proc:
 > $\mathrm{GetNumber}\left(\right)$