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 FOpenThe 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. CloseThe 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.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. End-of-fileOften 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 dataFlush filenumber% shall cause any unwritten data for that file number to be written to the file.Error checkingIn 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. Example: Execution example: Name of file to read ?recursive.bas ' This file is part of nuBASIC Recurs 4 ' ---------------------------------------------------------------- Sub Recurs( x% ) ' ---------------------------------------------------------------- Print x%; " "; x%=x%+1 If x%>10 Then Exit Sub Recurs x% End Sub |