Question: how to close file opened by readline("file_name")?

This behavior by Maple is completely wrong if you ask me.

But given this is how Maple works, the question I have is how to explictly close a file opened by call to readline()?

I have a proc(), where inside it, it wants to read say first 3 lines by calling readline("input.txt") on a file. Then the proc() returns back to caller.

The first call reads the first 3 lines from the file OK.

I expected all resources to be removed after the call returns, including any files opened to be automatically closed. So next time proc() is called, I expected the first 3 lines to be read again.

It turns out the next time  proc() is called, now lines 4,5,6 are read. This is becuase the file remained open between calls!  Only way to close the file is to call restart() on the whole session. But I do not want to do this. 

Looking at help, I see no method to explicity close the file other than reading all the lines.

How does one explicitly close a file opened by readline() without reading the whole file?

Here is example

restart;

foo := proc()
local n,current_line;

   for n from 1 to 3 do
       current_line:= readline("input.txt");
       print("line read is ",current_line);
   od;
end proc;

And now the above is called as follows

currentdir("C:\\test"); #change as needed
foo()
                   "line read is ", "line 1"
                   "line read is ", "line 2"
                   "line read is ", "line 3"

Next call

foo()
                   "line read is ", "line 4"
                   "line read is ", "line 5"
                   "line read is ", "line 6"

Where input.txt is 

line 1
line 2
line 3
line 4
line 5
line 6

I know how to do all this using fopen() and fscanf() and explicit fclose(). I also know I can use FileTools package.

But wanted to check first if readline() will do what I wanted more easily if I can figure how to close the file explicitly.

Please Wait...