Disk File Input/Output |
String and numeric expressions can replace any of the parameters:
OPEN M$,FN, DN$+ "OUTPUT"
This statement opens a file for either input or output, depending on the value of M$ (it must be either "I" or "0") and assigns the file number represented by FN (it must be from 1 to 6). The name of the file is the value of the string variable DN$ (it should be ":FO:" through ":F9:", depending on how many drives you have, or null) followed by the six characters OUTPUT.
NOTE
Remember that opening a file for output destroys an existing file of the same name.
Writing to a Sequential File
After you open a sequential file for output, you can write data to it with the PRINT statement. If you open an existing file for output, its contents are deleted. To specify that a PRINT statement is being used to write to a sequential disk file, the file number (preceded by #, followed by a comma) follows the word PRINT.
To print a series of constants and variables to file 3:
PRINT #3, "Today'sdate is ";MO$;OA;",";YR
You can also use the full formatting capabilities of the PRINT USING statement:
PRINT #3 USING, "###,##;"234.41 ;81.20;4.68
Refer to Chapter 6 for further details of PRINT USING.
Reading from a Sequential File
After you open a sequential file for input, you can read data from it with the INPUT and LINE INPUT statements. To specify that these statements are being used to read from a sequential disk file, the file number (preceded by # and followed by a comma) follows the word INPUT or LINE INPUT.
The INPUT statement reads the specified number of values (numeric or string) from the disk file. Numeric values (in the disk file) can be separated by a blank, comma, carriage return, or line feed.
String values can be enclosed in quotation marks, or given as unquoted strings. Quoted strings terminate on the quote marks, while unquoted strings terminate on commas, carriage returns, line feeds, or if they exceed 255 characters in length.
Assume, for example, that disk file #2 contains the following data (a new line represents a carriage
"R1" ,200, "R2" ,2200, "R3" ,10000 "R4" ,.47
After the file is opened for input, the following INPUT statement would assign a string value to R$ and the subsequent numeric value to R:
INPUT #2,R$,R