file.open
Syntax
- i = file.open(s,irw,im)
Open a file for reading/writing. The file may be opened in a state to read/write/append information, and may be in FISH, ASCII, or binary mode.
Returns: i - The following conditions are denoted:
Arguments: s - file name
irw - Read/write mode identifier with the following values:
- 0: read access: file must exist
- 1: write access: existing file will be overwritten
- 2: write access: existing file will be appended
im - Type mode identifier with the following values:
- 0: FISH mode: read/write FISH variables. Only the data corresponding to the FISH variable, not the name of the variable, are read/written.
- 1: ASCII mode: read/write ASCII data. In a read operation, the data must be organized in lines, with CR/LF between lines.
- 2: Binary mode: read/write binary data.
Example
model new
model largestrain off
fish define setup
global a_size = 20
global IO_READ = 0
global IO_WRITE = 1
global IO_FISH = 0
global IO_ASCII = 1
global filename = 'junk.dat'
end
@setup
;
fish define io
array aa(a_size) bb(a_size)
;
; ASCII I/O TEST ------------------
local status = file.open(filename, IO_WRITE, IO_ASCII)
aa(1) = 'Line 1 ... Fred'
aa(2) = 'Line 2 ... Joe'
aa(3) = 'Line 3 ... Roger'
status = file.write(aa,3)
status = file.close
status = file.open(filename, IO_READ, IO_ASCII)
status = file.read(bb, a_size)
if status # 3 then
local oo = io.out(' Bad number of lines')
endif
status = file.close
;
; now check results...
loop local n (1,3)
if string.token(bb(n), 2) # n then
oo = io.out(' Bad 2nd item in loop ' + string(n))
exit
endif
endloop
;
if string.token.type(bb(3),4) # 3 then
oo = io.out(' Not a string')
exit
endif
;
; FISH I/O TEST -----------------
status = file.open(filename, IO_WRITE, IO_FISH)
local funny_int = 1234567
local funny_float = 1.2345e6
aa(1) = '---> All tests passed OK'
aa(2) = funny_int
aa(3) = funny_float
;
status = file.write(aa,3)
status = file.close
status = file.open(filename, IO_READ, IO_FISH)
status = file.read(bb, 3)
status = file.close
;
; now check results...
if type(bb(1)) # 3 then
oo = io.out(' Bad FISH string read/write')
exit
endif
if bb(2) # funny_int then
oo = io.out(' Bad FISH integer read/write')
exit
endif
if bb(3) # funny_float then
oo = io.out(' Bad FISH float read/write')
exit
endif
oo = io.out(bb(1)) ; (should be a good message)
command
sys "del junk.dat"
endcommand
end
;
@io
Was this helpful? ... | PFC 6.0 © 2019, Itasca | Updated: Nov 19, 2021 |