ReadLine |
Streams | |
Aptilis 2 |
ReadLine(Stream)
This stream command allows you to a get line of text from a stream,
from the current position.
Aptilis will gather all the characters from the specified stream
until it finds a line separator (LineFeed) '\n', ie. a character which ASCII
code is 10.
The '\n' character will not be removed and you will get it at the end
of your line. In files generated by a PC (as opposed to a Unix system)
each '\n' character may be preceded by a '\r' or ASCII character 13, also
called Carriage Return'. This too will have been left at at the end
of your line, before the '\n'. To get rid of those, you may use the
trim
command.
See Open for a primer on Aptilis streams and examples.
Return Value:
A line of text, (which may only contain "\n" or "\r\n" if it's a blank line)
and an empty string "" at the end of the file.
Example 1:
We're using a file called 'rltest.txt' that contains:
Archer
Kirk
Picard
Cisko
Janeway
h = open("rltest.txt", "read") repeat l = readLine(h) $ if len(l$) = 0 break end if print("{{{{", l$, "}}}}\n") until 0 close(h)Result: (note the }}}} on the following line, because the '\n' has been preserved.)
{{{{Archer }}}} {{{{Kirk }}}} {{{{Picard }}}} {{{{Cisko }}}} {{{{Janeway }}}} |
Example 2:
This time instead of a file, we're going to be using a
socket. However sockets usually connect you to another
program on another machine. This program will most likely follow
a protocol. In other words we have to indicate what we want,
that's what the write is for here. Port 80 is the usual http
port.
h = open("socket://www.cnn.com:80") // http speak to indicate we want the default page at root level. write(h, "Get / HTTP/1.0\r\n\r\n") repeat l = readLine(h) $ if len(l$) = 0 break end if print("{{{{", l$, "}}}}\n") until 0 close(h)Result: (note the }}}} on the following line, because the '\n' has been preserved.)
{{{{HTTP/1.0 200 OK }}}} {{{{Date: Tue, 29 Jan 2002 21:41:08 GMT }}}} {{{{Last-Modified: Tue, 29 Jan 2002 21:41:09 GMT }}}} {{{{Expires: Tue, 29 Jan 2002 21:42:09 GMT }}}} {{{{Cache-Control: private, max-age=60 }}}} {{{{Content-Type: text/html }}}} {{{{ }}}} {{{{<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> }}}} {{{{<html lang="en"> }}}} {{{{<head> ..... ok I skipped a bit here for legibility ....... {{{{</body> }}}} {{{{</html> }}}} |
See also:
Open,
Close,
Write,
Write,
SetPosition,
GetPosition
and:
LoadFile and
SaveFile.