Definition: | object x = gets(integer fn) |
Description: | Get the next sequence (one line, including '\n') of characters from file or device fn. The characters will have values from 0 to 255. The atom -1 is returned on end of file. |
Comments: | Because either a sequence or an atom (-1) might be returned, you
should probably assign the result to a variable declared as object.
After reading a line of text from the keyboard, you should normally output a \n character, e.g. puts(1, '\n'), before printing something. Only on the last line of the screen does the operating system automatically scroll the screen and advance to the next line. The last line in a file might not end with a new-line '\n' character. When your program reads from the keyboard, the user can type control-Z, which the operating system treats as "end of file". -1 will be returned. In SVGA modes, DOS might set the wrong cursor position, after a call to gets(0) to read the keyboard. You should set it yourself using position(). |
Example 1: |
sequence buffer object line integer fn -- read a text file into a sequence fn = open("myfile.txt", "r") if fn=-1 then puts(1, "Couldn't open myfile.txt\n") abort(1) end if buffer = {} while 1 do line = gets(fn) if atom(line) then exit -- -1 is returned at end of file end if buffer = append(buffer, line) end while |
Example 2: |
object line puts(1, "What is your name?\n") line = gets(0) -- read standard input (keyboard) line = line[1..length(line)-1] -- get rid of \n character at end puts(1, '\n') -- necessary puts(1, line & " is a nice name.\n") |
See Also: | getc, puts, open |