5 Data input and output

5.1 Character string

5.1.1 String constant

When a character string is described in a program, it is enclosed in double quotation marks. The character string enclosed in double quotation marks is called a string constant. When a double quotation mark is included in the character-string constant, it is doubled like "".
Example 54

10 PRINT "Say ""Hello!""" 
20 END 

5.1.2 String variable

A variable having the character string as a value is called a string variable. In BASIC, any string variable is named using a $ (dollar symbol) at the end.

5.1.3 String concatenation

For character strings, concatenation operation is defined. String concatenation is expressed by writing & between the character strings.
Example 55.

10 LET s$="Brown"
20 LET s$="Mr. " & s$
30 PRINT s$ 
40 END

5.1.4 STR$ function

The built-in function STR$(x) converts a numerical value x to a string. No blank character shall be included before or after the numerical value.
The following program converts the input value to a binary representation and displays it in such a way that the most significant digit comes to the left.
例 56

10 INPUT n
20 LET s$ = ""
30 DO UNTIL n=0
40    LET r = MOD(n,2)
50    LET s$ = STR$(r) & s$
60    LET n = (n-r)/2
70 LOOP
80 PRINT s$
90 END

A character string that has no character is called a null string. a null string is expressed by "".
In the above program, s$ is made a null string first, and each time the input value is divided by 2, the remainder is converted to a character string and then concatenated to the left of s$.

5.2 DATA statement

5.2.1 READ statements and DATA statements

BASIC is provided with syntax to describe data in a program.
Data are described in a DATA statement delimiting with commas.
The instruction to read data into variables is a READ statement. When two or more variables are written in a READ statement, they are delimited with commas.
Data items are read in the variables in the sequence how they are written in a DATA statement.
Example 57.

10 DATA 1,2,3,4,5,6,7,8,9,10
20 FOR i=1 TO 5
30    READ a,b
40    PRINT a,b
50 NEXT i
60 END

When all data cannot be written in one line, they can be written in multiple DATA statements.
In such a case, no comma is written at the end of a line. (If so, a syntax error shall occur.)
For example, the DATA statement in the above program can be split into three statements as shown below.

10 DATA 1,2,3,4
12 DATA 5,6,7,8
14 DATA 9,10

5.2.2 READ IF MISSING THEN

In the above example, 5 sets of data, 2 pieces per set, are prepared, and so the FOR~NEXT loop is repeated 5 times to read the data, but it is troublesome if the data quantity is indefinite or unknown.
Full BASIC is provided with an instruction to read an indefinite quantity of data.
If “IF MISSING THEN EXIT DO: ” is written between “READ” and the first variable name in a READ statement in a DO~LOOP, the repetition of the DO~LOOP shall be finished when data is no longer available.
The following example is a program to calculate the average of data written in a DATA statement.
Example 58.

100 DATA 34,71,85,80,58,49,52,13
110 LET n=0
120 LET t=0
130 DO
140    READ IF MISSING THEN EXIT DO: x
150    LET t=t+x
160    LET n=n+1
170 LOOP
180 PRINT t/n
190 END

5.2.3 RESTORE statement

A RESTORE statement makes READ statements read data from the first again.
The following program calculates the difference from the mean, or the deviation, for each data. Since the mean must be calculated before the calculation of the deviation, the data should be read twice.
Example 59.

100 DATA 34,71,85,80,58,49,52,13
110 LET n=0
120 LET t=0
130 DO
140    READ IF MISSING THEN EXIT DO: x
150    LET t=t+x
160    LET n=n+1
170 LOOP
180 LET m=t/n         ! the mean
190 RESTORE
200 FOR i=1 TO n
210    READ x
220    PRINT x-m      ! the deviation
230 NEXT i
240 END

5.3 Files

5.3.1 OPEN statement and CLOSE statement

It is posible to input data from a text file, or output results to a text file. A text file is the simplest type a file, which can be read and written with NotePad of Windows or other text editors.
When a file is used, it is assigned to a virtual existence called a channel. Channels are identified by positive integers. Commonly, small numbers such as 1 and 2 are used sequentially.
The instruction to assign a file to a channel is an OPEN statement. An OPEN statement is written in the following form.
OPEN #Channnel_Number : NAME File_Name
In the above, Channnel_Number is a numeric expression and File_Name is a string expression.
When the use of a file is completed, a CLOSE statement is executed. A CLOSE statement is written in the following form.
CLOSE #Channel_Number

5.3.2 Input from a file

If the same content as the input reply to an INPUT statement is written in a text file, data can be continuously input from the file. The input reply corresponding to one execution of an INPUT statement corresponds to one line in a text file, but we can write an input reply in multiple lines, writing a comma at the end of a line.
When input is made from a file, an INPUT statement is written in the following form. INPUT #Channel_Number: variable, variable, …, variable
For example, if you want to input data from a file named “DATA1.TXT” on the drive A, what you should make is such a program as shown below. Example 60

10 OPEN #1: NAME "A:\DATA1.TXT"
20 FOR i=1 TO 4
30    INPUT #1:s$,n
40    PRINT s$,n
50 NEXT i
60 CLOSE #1 
70 END

“A:DATA1.TXT”in the above is a file name on Windows. The way to specify the file depends on OS.
In DATA1.TXT, a string constant and a numerical constant corresponding to the input of line 30 are written in a line delimiting with a comma, and such four lines are to be prepared as follows.

"Yamada", 78
"Suzuki", 90
"Sakai", 100
"Okano", 76

They can also be written in 8 lines by line-feeding just after the comma.
If an INPUT statement with IF MISSING THEN EXIT DO is used in a DO~LOOP, it is possible to prepare a program that copes with an indefinite number of input.
In this case, “IF MISSING …” is written to follow the channel number with a comma.
例 61

10 OPEN #1: NAME "A:DATA1.TXT"
20 DO
30    INPUT #1, IF MISSING THEN EXIT DO: s$,n
40    PRINT s$,n
50 LOOP
60 CLOSE #1 
70 END

5.3.3 Output to a file

There are two methods, overwriting and appending, in writing to a file. Overwriting erases any existing content in the specified file and replaces it with new content. Appending writes to the end of the existing text.
For overwriting, an ERASE statement shall be executed just after the OPEN statement is executed. If the file of the specified name does not exist, a new one shall be created. It is no problem to execute an ERASE statement for an empty file. Example 62

10 OPEN #2:NAME "A:DATA2.TXT"
20 ERASE #2
30 FOR x=1 TO 10
40    PRINT #2: x,SQR(x)
50 NEXT x
60 CLOSE #2
70 END

Outputting to a text file is done in the same form as to the screen. That is, it is the same form as the contents of a file in which the contents of the text output window are stored. Since blanks are output between items, it is not suitable to re-reading with BASIC, but it can be read into a spreadsheet such as EXCEL if spaces is specified for the delimiter.
In case of appending, change line 20 to

20 SET #2: POINTER END

5.3.4 File of internal record type

If data with more than one item is output as a text file, it cannot be re-read with BASIC, but if internal record type is used, the data output with BASIC can be re-read with BASIC. In order to use internal record type, “RECTYPE INTERNAL” is added to the end of an OPEN statement, a WRITE statement is used for output in place of a PRINT statement, and a READ statement is used for input in place of an INPUT statement.
Since the actual form of internal record type of Decimal BASIC is a comma-separated text (CSV), it can be used for data exchange with a spreadsheet program, etc.
Example63.
Outputting in internal record type

10 OPEN #1:NAME "A:DATA2.CSV" ,RECTYPE INTERNAL
20 ERASE #1
30 FOR x=1 TO 10
40    WRITE #1: x,SQR(x)
50 NEXT x
60 CLOSE #1
70 END

Example 64.
Reading a file of internal record type

10 OPEN #1: NAME "A:DATA2.CSV" ,RECTYPE INTERNAL
20 DO
30    READ #1, IF MISSING THEN EXIT DO: x,y
40    PRINT x,y
50 LOOP
60 CLOSE #1 
70 END

READ statements and WRITE statements can be used in a file of not internal record type, but in such a case, the effect is same as for INPUT statements and PRINT statements.




Next

Back