Binary File Operations

Use these commands to perform low level file operations. These are useful for reading and writing all data types in both big and little endian formats.  When performing read and write access to the memory(x) array in common formats use the  Memory Statements.  To read and write sequential values using ASCII text files,  use the  FINPUT and FPRINT statements.
 
 

Statement
Purpose
FOPEN Opens a file for binary R/W access.
FCLOSE Closes a binary file.
FSEEK Sets the file pointer.
FWRITE Writes data to a file.
FREAD Reads data from a file.

Example:

# Open a file for reading and writing
# Give it an ID of 1 (variable of)
 $myfile = $temppath "\test.bin"
 of = 1
 fopen of $myfile

# Write text - 29 chars
 $a = "This is a sample text string"

 fwrite of $a 29

# Write a byte
 bytval = 116
 fwrite of bytval u8

# Initialize a table of 1000 square roots and
# write to the file as big endian 64 bit floats
 a = 1
 loop begin 1000
  memory(a) = sqrt(a)
  a = a + 1
 loop end
 fwrite of memory(1) 1000 f64 b

# Change our mind and replace some of
# the file contents at file location 10
 fseek of 10
 $a = " `changed` "
 fwrite of $a 11
 
# seek to the end of the file and write
# the square root table as 16 bit integers

 fseek of 8030
 fwrite of memory(1) 1000 u16 b

# Read what we have written
 clear

# Text at file pos 0
 fseek of 0
 fread of $a 29
 print "Text: " $a

# The f64 square root of 398
 fpos = 30 + (397 * 8)
 fseek of fpos
 fread of num f64 b
 print "Number at " fpos " is " num

# The integer square root of 398
 fpos = 30 + (1000 * 8) + (397 * 2)
 fseek of fpos
 fread of num u16
 print "The little endian integer at " fpos " is " num " (wrong)"

 fseek of fpos

 fread of num u16 b
 print "The big endian integer at " fpos " is " num " (correct)"

# Always close the file when done
 fclose of

exit