File Operations

There are two types of files for storing data in nuBASIC – namely, text files and binary files. 
Files opened both for text and binary access may be read or written, beginning at any location within the file.

Open and FOpen

The Open statement is used to open files - that is, it makes files available so that you can read or write to them. 
This statement is present in many BASIC dialects, and in nuBASIC is implemented with the most commonly accepted syntax.
nuBASIC provides also a second statement to open files FOpen. 
This statement, as the name suggests, comes from C stdio fopen API.


The Close instruction is used to close files,

In order to avoid possible memory problems and to increase efficiency, it is prudent to close a file after a program is finished with it.

Print# / Write# 

The Print#(or Write#) instruction writes data to a file - the data is written to the file whose number follows "#". 
It works like Print, except that the information is sent to the file instead of printed to the screen. 
Of course a file must be opened before anything can be written to it - so always an Open/FOpen command must precede a Print # statement.

Input #

Reads from a file data that has been written with the Print#/Write# instruction.
The file must first be opened in input-mode before this command can be used.


Reads data from the specified file number. 
The file must first be opened in input-mode before this command can be used. 


Often when reading data from a file a program does not know beforehand the length of the file. If the reading is done from within a loop, there must be a way to stop the loop when all the data is read - otherwise, nuBASIC fails when asked to read more data after the end of the file has been reached. 

For this purpose nuBASIC employs a function Eof(filenumber%), that checks whether the last reading of the file used up all the data in the file. 

The function Eof(filenumber%) checks the end-of-file indicator. If no error has occurred on filenumber%, Eof returns 1 If end- is reached, else it returns 0. 

If filenumber% is invalid, Eof sets errno to 22 and returns -1.

Flushing data

Flush filenumber% shall cause any unwritten data for that file number to be written to the file.

Error checking

In order to test for an error on a file nuBasic provides the function FError(filenumber%). 
If no error has occurred on file, FError returns 0. 
If filenum is invalid, FError sets errno to 22 and returns -1. 
Otherwise, it returns a nonzero positive value.

Rem Read a text file showing its content on the screen
Input "Name of file to read "; filename$
Open filename$ For Input Access Read As #1
If FEerror(1) Then
   Print "Cannot open '"; filename$; "'"
End If

While Not(FError(1)) And Not(Eof(1))
   Input# 1, line$
   Print line$
End While
Close #1

Execution example:
Name of file to read ?recursive.bas
' This file is part of nuBASIC

Recurs 4

' ----------------------------------------------------------------
Sub Recurs( x% )
' ----------------------------------------------------------------
    Print x%; " ";
    If x%>10 Then Exit Sub
    Recurs x%
End Sub