Q(uick)BASIC Statement: READ

Quick View

READ

An I/O statement that reads values from a DATA statement and assigns the values to variables

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • DATA constant[,constant]…
  • READ variablelist
  • RESTORE [line]
Description/Parameter(s)

DATA specifies values to be read by subsequent READ statements.
READ reads those values and assigns them to variables.
RESTORE allows READ to reread values in specified DATA statements.

constant One or more numeric or string constants specifying the data to be read. String constants containing commas, colons, or leading or trailing spaces are enclosed in quotation marks (" ").
variablelist One or more variables, separated by commas, that are assigned data values. Variable names can consist of up to 40 characters and must begin with a letter. Valid characters are A-Z, 0-9, and period (.).
line The label or line number of a DATA statement. If line is omitted, the next READ statement reads values in the first DATA statement in the program.
  • DATA statements can be entered only at the module level. They cannot be used in procedures.
Example
FOR i% = 1 TO 3 READ a%, b$ PRINT a%, b$ RESTORE NEXT i% DATA 1, "Repeat"
Syntax
  • READ variablelist
Description/Parameter(s)

A variablelist is a series of valid BASIC variables separated by commas.

READ statements are always used with DATA statements. READ assigns DATA values to variables on a one-to-one basis. These variables may be numeric or string. Attempting to read a string value into a numeric variable produces a run-time syntax error. Reading a numeric value into a string variable does not produce an error, but stores the value as a string of numerals.

Values read into integer variables are rounded before the value is assigned to the variable. Reading a numeric value too large for a variable produces a run-time error.

String values read into fixed-length string variables are truncated if the string is too long. String values shorter than the string-variable length are left-justified and padded with blanks.

Only individual elements of a record variable may appear in a READ statement.

A single READ statement may use one or more DATA statements (they will be used in order), or several READ statements may use the same DATA statement. If there are more variables in variablelist than there are values in the DATA statement or statements, an error message is printed that reads "Out of DATA." If there are fewer variables than the number of elements in the DATA statement or statements, subsequent READ statements begin reading data at the first unread element. If there are no subsequent READ statements, the extra items are ignored.

Use the RESTORE statement to reread DATA statements.

Example

This example shows how you can use a READ statement to read information into the user-defined type Employee.

TYPE Employee EmpName AS STRING * 35 SocSec AS STRING * 11 JobClass AS INTEGER END TYPE CLS ' Clear screen DIM ThisEmp AS Employee DATA "Julia Magruder","300-32-3403",3 DATA "Amelie Reeves Troubetzkoy","777-29-3206",7 ' Read first data input line and verify by printing data READ ThisEmp.EmpName, ThisEmp.SocSec, ThisEmp.JobClass PRINT "Employee is "; ThisEmp.EmpName PRINT "Employee's social security number is "; ThisEmp.SocSec PRINT "Employee's job class is"; ThisEmp.JobClass PRINT ' Print blank line ' Read second data input line and verify READ ThisEmp.EmpName, ThisEmp.SocSec, ThisEmp.JobClass PRINT "Employee is "; ThisEmp.EmpName PRINT "Employee's social security number is "; ThisEmp.SocSec PRINT "Employee's job class is"; ThisEmp.JobClass

Sample Output:

Employee is Julia Magruder Employee's social security number is 300-32-3403 Employee's job class is 3 Employee is Amelie Reeves Troubetzkoy Employee's social security number is 777-29-3206 Employee's job class is 7
Syntax
  • READ variablelist
Description/Parameter(s)
variablelist A series of BASIC variables that receive the data from a DATA statement. The variables are separated by commas and can be string or numeric. See Details for information on data type conversion performed by BASIC.
  • Each variable in a READ statement receives its value from some DATA statement. Which value the variable receives depends on how many values have previously been read. The values of all DATA statements in a module can be considered as a single list of values. Each value in this list is assigned in turn to the variables specified in READ statements. It doesn't matter how many values are specified in a given DATA statement or how many variables are specified in a READ statement. If you attempt to read more values than are specified in all the statements combined, BASIC generates the error message, "Out of Data."
  • The following table describes what happens when you try to read data of one data type into a variable with a different data type:
When you try to read this: Into this: The result is:
String value Numeric variable A run-time error.
Numeric value String variable The value is stored as a string of numerals (no error produced).
Any numeric value Integer variables The value is rounded before it is assigned to the variable.
Numeric value A variable not large enough to handle the numeric variable. A run-time error.
String value Fixed-length string variables Truncated if the string is too long; left-justified and padded with blanks if the string is shorter than the variable.
  • Only individual elements of a record variable can appear in a READ statement.
  • Use the RESTORE statement to reread DATA statements.
Examples

The following example displays the current date by converting the date returned by the DATE$ function. The names of months are stored in DATA statements and assigned to a string variable using the READ statement.

'Get the date. C$ = DATE$ 'Use VAL to find the month from the string returned by DATE$. FOR I% = 1 TO VAL(C$) READ Month$ NEXT DATA January, February, March, April, May, June, July DATA August, September, October, November, December 'Get the day. Day$ = MID$(C$, 4, 2) IF LEFT$(Day$, 1) = "0" THEN Day$ = RIGHT$(Day$, 1) 'Get the year. Year$ = RIGHT$(C$, 4) PRINT "Today is "; Month$; " "; Day$; ", "; Year$

Sample Output:

Today is September 21, 1989

This example shows how null data items are handled:

RESTORE NullData NullData: DATA abc,,def DATA 1,,2 READ A$, B$, C$ 'B$ = "" PRINT A$, B$, C$ PRINT READ A, B, C 'B = 0 PRINT A, B, C

Sample Output:

abc def 1 0 2