gets
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. |
pwa/p2js: | Not supported. I would hope it is pretty obvious that you cannot read a disk file from within a browser, and should you be thinking of keyboard input, that would have to be (say) KEY, or perhaps more relevantly a VALUE_CHANGED callback on an gText. |
Comments: | Because either a sequence or an atom (-1) might be returned, you
should probably assign the result to a variable declared as object.
The last line in a file might not end with a new-line '\n' character. |
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") |
Implementation: | via :%opGets in builtins\VM\pfileioN.e (an autoinclude) - be warned however it is low-level complicated stuff that you do not need to know. |
See Also: | getc, puts, open |