File Type Sequential
Jump to navigation
Jump to search
![]() |
![]() |
![]() |
![]() |
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.
Syntax
Open and Close File
To OPEN a file in read mode:
- open fileName$ for INPUT as #handle
To OPEN a file in write mode:
- open fileName$ for OUTPUT as #handle
To OPEN a file in append mode:
- open fileName$ for APPEND as #handle
To CLOSE a file
- close #handle
Reading and Writing Data
To WRITE data to the file:
- print #handle, data to write '(Obsolete form)
- #handle data to write '(Preferred form - without 'print' and comma)
To READ data from the file into the program:
- input #handle, var ' Reads data up to a comma or carriage return.
- line input #handle, var ' Reads full line of data up to carriage return - line feed sequence, including embedded commas.
- var$ = INPUT$(#handle, n) ' Reads n number of characters from file. Be warned that it will read characters even they are the delimiters.
- 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
Below find a table of the relation between file content type and preferred access mode.
Content | Mime Type | Typical Extension | Access Mode | Reading | Writing | Open Mode | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Plain Text | text/plain | ini, txt, log | Sequential | INPUT | read or write | |||||||
Random Access | Random | GET | PUT | read and write (*) | ||||||||
Anything else | bmp, gif, doc, etc. | Binary | INPUT$() | read and write (*) |
(*) The file must be WRITE-able. So attempting to open a read-only file (or file from read-only media, like CD) for RANDOM or BINARY will fail.
Example
Input
'create a sample file open "test.txt" for output as #1 #1 "123 Sesame Street, New York, NY" close #1 '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
Output
'create a sample file open "test.txt" for output as #1 'write some data with line delimiters #1 "line one " #1 "line two " 'write some data without line delimiters - will be INPUT as single instance (whole long line) ' "item three item four " #1 "item three "; #1 "item four " 'write some data with comma delimiter - will be INPUT as separate instances ' "item five " "item six " #1 "item five ";","; #1 "item six " 'more data with line delimiters added #1 "item seven" #1 "done" close #1 '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
Append
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
Useful Procedures
' Place a useful function using this keyword here