Q(uick)BASIC Function: INPUT$
Quick View
INPUT$
A file I/O function that returns a string of characters read from the specified file
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- INPUT$(n[,[#]filenumber%])
Description/Parameter(s)
n | The number of characters (bytes) to read. |
filenumber% | The number of an open file. If filenumber% is omitted, INPUT$ reads from the keyboard. |
Example
OPEN "TEST.DAT" FOR OUTPUT AS #1
PRINT #1, "The text"
CLOSE
OPEN "TEST.DAT" FOR INPUT AS #1
PRINT INPUT$(3, 1) 'Print first 3 characters.
CLOSE
See also:
Syntax
- INPUT$(n[,[#]filenumber])
Description/Parameter(s)
The n is the number of characters (bytes) to read from the file. The filenumber is the number used to open the file.
If the file is opened for random access, the argument n must be less than or equal to the record length set by the LEN clause in the OPEN statement (or less than or equal to 128 if the record length is not set). If the given file is opened for binary or sequential access, then n must be less than or equal to 32,767.
If the filenumber is not specified, the characters are read from the standard input device. (If input has not been redirected, the keyboard is the standard input device).
You can use the DOS redirection symbols " "(<, >, or >>) or the pipe symbol ( | ) to redefine the standard input or standard output for an executable file created with BASIC. (See your operating system manual for a complete discussion of redirection and pipes.)
No characters are echoed on the screen. All control characters are passed through except CTRL+BREAK, which interrupts execution of the function.
Example
This example prints a file on the screen using INPUT$ to read one character at a time, then converting the character as necessary and displaying it.
Tip: You must supply a text file when you run this example. Use a text file you have already created, create a file with a text editor, or specify the README.DOC text file.
'ASCII codes for tab, and line feed.
CONST HTAB = 9, LFEED = 10
CLS ' Clear screen
INPUT "Display which file"; Filename$
OPEN Filename$ FOR INPUT AS #1
CLS
DO WHILE NOT EOF(1)
' Input a single character from the file.
S$=INPUT$(1,#1)
' Convert the character to an integer and
' turn off the high bit so WordStar(R) files
' can be displayed.
C=ASC(S$) AND &H7F
' Is it a printable character?
IF (C >= 32 AND C <= 126) OR C = HTAB OR C = LFEED THEN
PRINT CHR$(C);
END IF
LOOP
END
See also:
Syntax
- INPUT$(n[,[#]filenumber%])
Description/Parameter(s)
- If the file is opened for random access, the argument n must be less than or equal to the record length set by the LEN clause in the OPEN statement (or less than or equal to 128 if the record length is not set). If the given file is opened for binary or sequential access, n must be less than or equal to 32,767.
Usage Notes
- If the filenumber% is not specified, the characters are read from the standard input device. (If input has not been redirected, the keyboard is the standard input device).
- You can use the DOS redirection symbols (<, >, or >>) or the pipe symbol ( | ) to redefine the standard input or standard output for an executable file created with BASIC. (See your operating system manual for a complete discussion of redirection and pipes.)
- Unlike the INPUT # statement, INPUT$ returns all characters it reads.
Example
This example uses the INPUT$ function to read one character at a time from a file. The input character is converted to standard printable ASCII, if necessary, and displayed on the screen.
Note: You must supply a text file when you run this example. Use a text file you have already created, create a file with a text editor, or specify the README.DOC text file.
'ASCII codes for tab, and line feed.
DEFINT A-Z
CONST HTAB = 9, LFEED = 10
CLS 'Clear screen.
INPUT "Display which file"; Filename$
OPEN Filename$ FOR INPUT AS #1
DO WHILE NOT EOF(1)
'Input a single character from the file.
S$ = INPUT$(1, #1)
'Convert the character to an integer and
'turn off the high bit so WordStar files
'can be displayed.
C = ASC(S$) AND &H7F
'Is it a printable character?
IF (C >= 32 AND C <= 126) OR C = HTAB OR C = LFEED THEN PRINT CHR$(C);
LOOP
END
See also: