'2018-03-12 T 19:16:09
'file demo
https://msdn.microsoft.com/en-us/library/634ca0c2.aspx
uses corewin
sys f
int le
f=fopen "t.txt","r" 'some text to read
print f
char s[512]
le=fread strptr(s),1,256,f
print left(s,le)
fclose f
'2018-03-13 T 07:47:19
'random access read/write
'https://docs.microsoft.com/en-gb/cpp/c-runtime-library/file-handling
'
$filename "t.exe"
'uses rtl64
uses corewin
'
'EXAMPLE RECORD
type xyzlabel
single x,y,z
wchar s[64]
end type
'
sys hf
int le
dim xyzlabel ww
'
'PUT SOME DATA INTO RECORD
ww={ 1.5 , 2.5 , 3.5 , "labelled point in space" }
hf=fopen "t.txt","w" 'CREATING / WRITING
if hf
int fd=_fileno hf
'print _filelength fd
fseek hf,sizeof(ww)*10,0 'POSITION 11TH RECORD
le=fwrite @ww,sizeof ww,1,hf
fclose hf
print "written: " ww.x " , " ww.y " , " ww.z " " ww.s
end if
'
'
dim xyzlabel rr 'BLANK RECORD TO RECEIVE DATA
'
hf=fopen "t.txt","r+" 'READING AND WRITING
if hf
int fd=_fileno hf
'a=_SetMode fd,0x8000 '_o_binary default
print _filelength fd
fseek hf,sizeof(rr)*10,0 '11TH RECORD IN FILE
le=fread @rr,sizeof rr, 1, hf
fclose hf
print "read " rr.x " , " rr.y " , " rr.z " " rr.s
end if
'/*
'2018-03-14 T 11:34:15
'READING WHOLE FILE
% filename "t.exe"
'use rtl64
use corewin
string s
sys e,f
'EQUIVALENT TO: S=GETFILE "T.TXT"
f=fopen "t.txt","r" 'open for reading (default binary)
print f 'file handle or null
fseek f,0,2 'end of file
e=ftell f 'get position
print e 'length of file
fseek f,0,0 'beginning of file
s=nuls e 'create buffer to fit
fread s,1,e,f 'load buffer
fclose f 'close file
'
print s
'print cast(wstring) s
... I can always write what I need with bc9 and let it do the translation for OxygenBasic. :)
Oh, I see.Mike,
Have you ever tried to retarget the translator for anything but BCX?
I found this syntax much easier than the verbose PB's
OPEN "text.txt" FOR INPUT As FP1
uses corewin
function LoadFile(string name, any*data, int e)
===============================================
sys f
f=fopen name,"r" 'open for reading
e=fread @data,1,e,f 'load data
fclose f 'close file
return e
end function
function SaveFile(string name, any*data, int e)
===============================================
sys f
f=fopen name,"w" 'open for writing
e=fwrite @data,1,e,f 'save data
fclose f 'close file
return e
end function
'TEST
=====
float v[100],w[100]
for i=1 to 100 : v[i]=i*1.5 : next
SaveFile "t.bin",v,bytesof v
LoadFile "t.bin",w,bytesof v
print w[51] '76.5