Liberty BASIC Help Online

Sequential Files
Sequential files are opened with the OPEN statement.  When they are no longer needed, or when the program ends, they must be closed with the CLOSE statement.
 
Sequential file access allows data to be read from a file or written to a file from beginning to end.  It is not possible to read data starting in the middle of the file, nor is it possible to write data to the file starting in the middle using sequential methods.
 
Data is read from a file opened for INPUT starting at the beginning of the file. Each subsequent input statement reads the next piece of data in the file.  Data is written to a file opened for OUTPUT with a PRINT statement starting at the beginning of the file, and each subsequent PRINT statement writes data to the end of the open file.  When a file is opened for APPEND, each PRINT statement writes data to the end of the open file.
 
Sequential File Access
 
INPUT
Files opened for INPUT can be read from.  They cannot be written to.  A file opened for INPUT must exist on disk, or the program will halt with an error.  See Testing For File Existence.
 
The INPUT statement reads a piece of data up to the next comma or carriage return.  The LINE INPUT statement reads a piece of data that contains commas that are not delimiters, and stops reading data at the next carriage return.  The INPUT$ statement reads data of a specified length from a file.  the INPUTTO$ statement reads data up to a specified delimiter.  Here is an illustration of the differences between the various forms of INPUT statements. 
 
Example Program:
'create a sample file
open "test.txt" for output as #1
print #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
 
'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
 
'INPUTTO$
open "test.txt" for input as #1
txt$ = INPUTTO$(#1, " ") 'use a blank space as delimiter
print "INPUTTO$ item is: ";txt$
close #1
 
 
 
Produces:
 
INPUT item is: 123 Sesame Street
LINE INPUT item is: 123 Sesame Street, New York, NY
INPUT$ item is: 123 Sesame
INPUTTO$ item is: 123
 
INPUT Multiple Items
Here is a short program which opens a text file and reads a line at a time, printing each line to the mainwin.
 
filedialog "Open ","*.txt", file$
if file$="" then end
 
open file$ for input as #1
while eof(#1) = 0
    line input #1, text$
    print text$
wend
close #1
 
'print a notice that the end of file is reached:
print:print:print "EOF"
 
 
 
OUTPUT
Files opened for OUTPUT can be written to sequentially.  If a file opened for OUTPUT does not exist, it will be created.  If the file does exist on disk, the previous contents will be overwritten, and therefore lost.  Care should be taken when opening files for OUTPUT so that critical data is not accidentally erased. See Testing For File Existence.
 
Data is written to a file opened for OUTPUT with a PRINT statement.  A line delimiter or carriage return is written to the file with each PRINT statement. The carriage return may be suppressed by ending the line of code with a semi-colon.
 
Example Program:
'create a sample file
open "test.txt" for output 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
 
 
'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
 
 
Contents of file:
 
line one
line two
item three item four item five
done
 
APPEND
Files opened for APPEND can be written to sequentially.  If a file opened for APPEND does not exist, it will be created.  If the file does exist on disk, any data written to the file with a PRINT statement will be added to the end.  Writing data to the file works in the same way when a file is opened for APPEND as when it is opened for OUTPUT, but rather than overwriting data contained in the file, the new data is appended to the end of the file, but does not overwrite data previously written to the file the last time it was opened as open for OUTPUT does.
.
 
Example Program:
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
 
File Copy
A file may be copied using sequential file operations.  The file to be copied is opened for INPUT.  The file that is to be a copy is then opened for OUTPUT. The contents of the original file are retrieved with the INPUT$ statement and written to the copy with the PRINT statement.  Both files are then closed.  Here is an example:
 
  open "mybytes.bin" for input as #original
  open "copybyte.bin" for output as #copy
  print #copy, input$(#original, lof(#original));
  close #original
  close #copy
  end
 


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/