Open+Sequential+Files

Opening Sequential Files
Files can be **OPENed** for reading or writing sequentially. Files opened for **binary** and files opened for **Input** can be read with one of the input statements or functions. Files opened for **binary**, files opened for **output** and files opened for **append** can be written to with the **print** statement.

Opened files must be closed with the Close statement when they are no longer needed. Information on opened files can be obtained with two functions. The end of the file can be retrieved with **EOF** and the length of the file can be retrieved with **LOF**.

The syntax for the statements are functions that manage sequential files is as follows:

OPEN expr$ FOR mode AS #handle //- Open a file or window #handle using expr$ and mode// INPUT #handle, var1[,var2...] //- Read data from file #handle into 1 or more vars.// LINE INPUT #handle, var1 //- Read an entire line of text from file #handle into var1. //INPUT$(#handle, n) //- Next n characters from open file #handle // EOF(#handle) //- Return 1 if open file #handle is at end, else return 0// LOF(#handle) //- Number of characters/bytes for open file #handle//

The following demonstration programs can be copied and pasted into a single file and run, if desired.

This demo shows how to write data to a sequential file opened for binary access.

code format="vbnet" open DefaultDir$ + "\public\filetest1.txt" for binary as #f print #f, "Test, line one." print #f, "Test, line two." close #f code

This demo shows how to write data to a sequential file opened for output.

code format="vbnet" open DefaultDir$ + "\public\filetest2.txt" for output as #f print #f, "Test two, line one." print #f, "Test two, line two." close #f code

This demo shows how to write data to a sequential file opened for append this line will be appended to the file each time it is run.

code format="vbnet" open DefaultDir$ + "\public\filetest2.txt" for append as #f print #f, "End of test." close #f code

This demo shows how to read data in loop with line input from sequential file opened for input.

code format="vbnet" print:print "Input Test File 2 - Line Input" open DefaultDir$ + "\public\filetest2.txt" for input as #f while not(eof(#f)) line input #f, a$ print a$ wend close #f code

This demo shows how to read data in loop with line input from sequential file opened for binary.

code format="vbnet" print:print "Binary Test File 1 - Line Input" open DefaultDir$ + "\public\filetest1.txt" for binary as #f while not(eof(#f)) line input #f, a$ print a$ wend close #f code

This demo shows how to read data in loop with input from sequential file opened for binary.

code format="vbnet" print:print "Binary Test File 1 - Input" open DefaultDir$ + "\public\filetest1.txt" for binary as #f while not(eof(#f)) input #f, a$ print a$ wend close #f code

This demo shows how to read data in loop with INPUT$(#handle, n) from sequential file opened for binary.

code format="vbnet" print:print "Binary Test File 1 - Input$" open DefaultDir$ + "\public\filetest1.txt" for binary as #f while not(eof(#f)) a$ = input$(#f, 1) print a$ wend close #f code

This demo shows how to read data in one hunk with INPUT$(#handle, n) and LOF from sequential file opened for binary.

code format="vbnet" print:print "Binary Test File 1 - Input$ and LOF" open DefaultDir$ + "\public\filetest1.txt" for binary as #f fileLen = LOF(#f) a$ = input$(#f, fileLen) print a$ close #f code