File Operations
Introduction
Input and output, whether to or from files, are supported on Flash and RAM devices. The files are accessed either in serial or random order. Currently, only ASCII text files are supported – both printable and non-printable characters.
OPEN
Opens an existing file or creates a new one with the name specified in the string expression. The file name should not exceed 8 characters plus extensions. Acceptable file extensions are:
PRG , DAT , TSK , CMP for permanent files on the Flash disk
REC for temporary files on the RAM disk
You can open a text file to read, write or append to the existing file according to mode flag.
“r” - open text file for reading
“w” - truncate to zero length or create text file for writing
“a” - append; open or create text file for writing at end-of-file
Use APPEND to add new lines at the end of the original contents of the file.Use WRITE to overwrite the previous file or to create a new file.
OPEN #, INPUT #, CLOSE, LOC
See serial port.
TELL
Returns current file pointer, offset from the beginning of the file. Value has meaning only for SEEK to move the file pointer within a file.
SEEK
Moves file pointer to specified location. Use with TELL.
File operations example:
program dim pos as long dim d1 as double dim s1 as string 'create file for writing Open "test.prg" mode ="w" as #1 print #1, "1.2345" close #1 'append to existing file Open "test.prg" mode ="a" as #1 print #1, PI close #1 'read from file Open "test.prg" mode ="r" as #1 'how many characters in the file? print "File contains ";loc(1);" characters" s1 = input$(#1) 'convert from string to double d1=val(s1) ?d1 'save current file pointer pos = tell(#1) ?input$(#1) 'rewind file seek (#1,pos) ?input$(#1) close #1 end program
Output:
File contains 31 characters 1.234500000000000e+00 3.141592653590000e+00 3.141592653590000e+0
File Types
A File Specification type indicates that the parameter takes a file name.
The file name may have up to 8 characters in the name with 3 characters in the extension. See the next table: USER files are Read/Load/Writable, the SYSTEM files are not.
Extension | Path | Hidden | Erasable | Read/Load/Write |
---|---|---|---|---|
TXT | RAMDisk | TRUE | FALSE | SYSTEM |
TMP | RAMDisk | TRUE | TRUE | SYSTEM |
BIN | RAMDisk | TRUE | FALSE | SYSTEM |
PRG | FlashDisk | FALSE | TRUE | USER |
O | FlashDisk | FALSE | TRUE | USER |
DAT | FlashDisk | FALSE | TRUE | USER |
SCE | RAMDisk | FALSE | TRUE | USER |
CSV | FlashDisk | FALSE | TRUE | USER |
ERR | RAMDisk | FALSE | FALSE | USER |
CNC | FlashDisk | FALSE | TRUE | USER |
LOG | FlashDisk | FALSE | FALSE | USER |
CFG | FlashDisk | TRUE | FALSE | USER |
SYS | FlashDisk | TRUE | FALSE | SYSTEM |
MAP | RAMDisk | TRUE | FALSE | SYSTEM |
REC | RAMDisk | FALSE | TRUE | USER |
CAM | FlashDisk | FALSE | TRUE | USER |
TSK | FlashDisk | FALSE | TRUE | USER |
CMP | FlashDisk | FALSE | TRUE | USER |
ATC | FlashDisk | TRUE | FALSE | SYSTEM |
PRT | RAMDisk | TRUE | TRUE | SYSTEM |
LIB | FlashDisk | FALSE | TRUE | USER |
MEM | RAMDisk | FALSE | TRUE | USER |
BAK | FlashDisk | FALSE | TRUE | USER |
PRO | FlashDisk | FALSE | TRUE | USER |
SAV | FlashDisk | FALSE | TRUE | SYSTEM |
NO EXTENSION | FlashDisk | TRUE | FALSE | SYSTEM |