Home > CBM > BASIC > Keywords > Input num | |||||||||||||||||||||||||||||||||||||||||
INPUT# file_number , variable [ , variable ] ...
Data input; typically from an external device.
This statement reads one or more values from the "file" associated with file_number and stores it/them in the specified variable(s). Each datum read from the file must be compatible with each variable's type. So a string variable must be specified if text is to be read. A numeric variable (integer or float) is sometimes used to read numeric values, but often a string variable will be used in these cases too. This is because if unexpected (non-numeric) characters appear when trying to input into a numeric variable, a FILE DATA ERROR is generated. Using a string variable, the (possibly mal-formed) data can be read without error and then tested and converted into a numeric value. Multiple items in the data stream of the file must be seperated with one of the following characters: a comma (,), a colon (:), or a carriage return (ASCII 13). Because of this, a string which is to contain any of those characters must be surrounded by double-quotes ("). There is no way to input a literal double-quote. Because of these issues, it is common to use GET# instead of INPUT#, although GET# is much slower. Although Commodore BASIC allows strings to be up to 255 characters in length, a string read using INPUT# may not be longer than system's input buffer (this varies by computer model). So if the system has an 80-character input buffer, then any "file" data which runs for more than 80 characters without a comma, colon, or carriage return will generate the not-quite-accurate error of STRING TOO LONG. These are more good reasons to avoid INPUT#. The "file" (or other input device) must have data available, or INPUT# may wait (possibly forever) until there is data. With some devices, an undefined value may be read immediately when data is not available. INPUT# may only be used as a statement in a program. Outside of a program it will generate ILLEGAL DIRECT error. Example:
The example is using the keyboard as the input "file". This is not typical, but it makes the example more concise. Otherwise a file would have to be created and then opened... and this would require knowledge of your hardware and media. Every CBM computer has a keyboard and they all work pretty much the same (good enough for the example).
© H2Obsession, 2014 |