12.1. Opening and creating files

[<<<] [>>>]

ScriptBasic handles the files the same way as any other BASIC type language. You open a file, read from it, write to it and finally close the file. To make a jumpstart see a simple example:

open "myfile.txt" for output as 1
print#1,"This is the first line of myfile\n"
close 1

open "myfile.txt" for input as 1 line input #1, a close 1 print a

This simple program opens the file named `myfile.txt' in the current directory, prints a single line into it, and closes the file. Next time it opens the file for reading, reads a line from it, closes the file and prints the line read from the file to the screen.

When you open a file you have to have a file number that you want the file associated with. In the example above this number is 1. This is called many times the "file number". Whenever you do something with an opened file you have to use the file number.

There are more things than reading from a file and writing to a file. You can read from a certain position on a file and you can write to a certain position. You can determine the length of a file and dates and times the file has, like last time it was accessed, modified or created.

[<<<] [>>>]