12.6. Reading and writing files

[<<<] [>>>]

@code PRINT #fn

To write into a file you can use the statement print in the form:

PRINT#fn, expression list

This is the same format as the ordinary print statement with the exception that a file number is specified that references an opened file. The expression list is printed into the file. If you want to start a new line in the file you can do it in one of two ways. You can write:

PRINT#fn, "\n"

or

PRINTNL#fn

In the first case the string containing a new line character is printed to the file. In the second case we wrote a print-new-line command. Note that you can print a new line character to the screen in three ways:

PRINTNL
PRINT
PRINT "\n"

The second format can not be used to print a new line into a file, the command

PRINT#fn

is syntactically incorrect. This is a restriction caused by the parser that ScriptBasic is built on. Later versions may allow this format.

To read from a file you have a statement and a function. The statement

LINE INPUT #fn, variable

will read a line from the file. The length of the line is not limited except that there should be enough memory. This statement will result a string in the variable that contains the line including the line terminating new line unless the line is terminated by the end of the file without closing new-line character.

For binary files that are not composed of lines the function input() should be used. This function reads from a file the given number of characters or bytes and results a string that contains the bytes read. It has the format:

Variable = input( NumberOfBytes, fn)

The number of bytes actually read and put into the variable by the function can be determined using the string function LEN(). If there are less number of bytes or records in the file from the actual file pointer until the end of the file than the required number of bytes the function results a shorter string containing the available bytes.


[<<<] [>>>]