Opening Files

Top  Previous  Next

Discussed in this topic: fd.filenum, fd.open, fd.fileopened, fd.maxopenedfiles.

 

You must first open a file in order to work with its data. The fd.open method opens a file with a specified name and "on" a file number currently selected by the fd.filenum property. All operations related to the file data are then performed by referring to the file number, not the file name. These operations include writing to and reading from files, removing data from files, searching within files, and closing files.

The concept of file numbers is not new — other operating systems, too, assign a number to the file when the file is opened. In Tibbo BASIC/C, you select the number you want to open the file on yourself. You do this by selecting a desired value for the fd.filenum property:

 

 

'will open two files 'on' numbers 3 and 5

 

fd.filenum=3

If fd.open("File1")<>PL_FD_STATUS_OK Then

  'some problem

End If

fd.filenum=5

If fd.open("TrestFile")<>PL_FD_STATUS_OK Then

  'some problem

End If

 

 

Whenever you want to work with one of the currently opened files, just set the fd.filenum to the number on which this file was opened (naturally, you need to somehow remember this number). And how many files can be opened concurrently? The fd.maxopenedfiles R/O property will tell you that. This value is platform-dependent. Your fd.filenum value can move between 0 and fd.maxopenedfiles-1.

When the file is opened "on" a certain file number, the fd.fileopened R/O property returns 1- YES when this file number is selected in the fd.filenum.

 

Any leading spaces in the file name you supply for fd.open are removed. After that, only the part up to the first space is processed — the rest of the string is ignored. The following three code lines all open the same file:

 

 

fd.open("File1")

fd.open("        File1") 'leading spaces will be removed

fd.open("File1 some more stuff") 'everything after the first space will be ignored too

 

 

Naturally, the file with the specified name must exist, or you get the 9- PL_FD_STATUS_NOT_FOUND error. You may not open the same file "on" two different file numbers — this will generate the 11- PL_FD_STATUS_ALREADY_OPENED error. You may reopen the same or another file "on" the same file number, but this can lead to the loss of (some) changes made to the previously opened file. To avoid this, close the file or use the fd.flush method before opening it again. Note that the fd.flush method does not depend on the current fd.filenum value and works globally on any most recently changed file.