BB___File_Type_Sequential

=Sequential Access Files= toc

Description
> A file type that requires the program to READ or WRITE data from the beginning of the file to the end, SEQUENTIALLY. It is not possible to retrieve a piece of data starting from the middle of the file, for example.

> Sequential files must be OPENed to be used and they must be CLOSEd when finished. Type of access is stated then file is opened:
 * Data is written to the file, OPENed for OUTPUT.
 * Data is read from the file OPENed for INPUT.
 * Data can be added to the end of the file OPENed for APPEND.

> A sequential file must exist on disk before it can be accessed. To create a file, it must be opened for OUTPUT or APPEND. Opening a file for INPUT before it is created will cause an error.

> NOTE: There is an important distinction to be made between writing to a file using OUTPUT and writing to a file using APPEND. Attempting to write a piece of data to an open file using OUTPUT will overwrite all the data presently saved in the file. To preserve the existing data when adding to the file, use APPEND, which will add the new data to the end of the file. Or, save the existing data in an array, make any changes, then write the entire array back to the file using OUTPUT.

Open and Close File
To OPEN a file in read mode: To OPEN a file in write mode: To OPEN a file in append mode: To CLOSE a file
 * 1) open //fileName$// for INPUT as //#handle//
 * 1) open //fileName$// for OUTPUT as //#handle//
 * 1) open //fileName$// for APPEND as //#handle//
 * 1) close //#handle//

Reading and Writing Data
To WRITE data to the file: To READ data from the file into the program:
 * 1) print //#handle//, //data to write// '(Obsolete form)
 * 2) #handle //data to write// '(Preferred form - without 'print' and comma)
 * 1) input //#handle//, //var// ' Reads data up to a comma or carriage return.
 * 2) line input //#handle//, //var// ' Reads full line of data up to carriage return - line feed sequence, including embedded commas.
 * 3) var$ = INPUT$(#handle, n) ' Reads n number of characters from file. Be warned that it will read characters even they are the delimiters.
 * 4) var$ = INPUT$(#handle, LOF(#handle)) ' Reads entire file into var$

NOTE: When reading, one should check for the end of the file with the EOF function. See **Input** example.

Hints
include component="page" page="BB___File_Type_Table" editable="1" wrap="1"

Input
code format="lb" 'create a sample file open "test.txt" for output as #1 close #1
 * 1) 1 "123 Sesame Street, New York, NY"

'INPUT open "test.txt" for input as #1 INPUT #1, txt$ print "INPUT item is: ";txt$ close #1

'INPUT till file ends print "-- Reading all items from a file" open "test.txt" for input as #1 while not(eof(#1)) INPUT #1, txt$ print "INPUT item is: ";txt$ wend print "-- file ended" close #1

'LINE INPUT open "test.txt" for input as #1 LINE INPUT #1, txt$ print "LINE INPUT item is: ";txt$ close #1

'INPUT$ open "test.txt" for input as #1 txt$ = INPUT$(#1, 10)  'read 10 characters print "INPUT$ item is: ";txt$ close #1 code

Output
code format="lb" 'create a sample file open "test.txt" for output as #1

'write some data with line delimiters
 * 1) 1 "line one "
 * 2) 1 "line two "

'write some data without line delimiters - will be INPUT as single instance (whole long line) '  "item three item four "
 * 1) 1 "item three ";
 * 2) 1 "item four "

'write some data with comma delimiter - will be INPUT as separate instances '  "item five "    "item six "
 * 1) 1 "item five ";",";
 * 2) 1 "item six "

'more data with line delimiters added close #1
 * 1) 1 "item seven"
 * 2) 1 "done"

'INPUT to see what we wrote open "test.txt" for input as #1 txt$ = input$(#1, lof(#1)) print "Contents of file: " print print txt$ close #1 code

Append
code format="lb" open "test.txt" for append as #1

'write some data with line delimiters print #1, "line one " print #1, "line two "

'write some data without line delimiters print #1, "item three "; print #1, "item four ";

'more data with line delimiters added print #1, "item five" print #1, "done" close #1 code

Useful Procedures
code format="lb" Place a useful function using this keyword here code