Thank you Charles for adding the msvcrt file IO.
A simple random access example.
The attached is the full O2RadAsm project including needed files. You can compile from the command line if desired.
James
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
' not needed with O2RadAsm
' $filename "cFileIO.exe"
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
use rtl64
#autodim off
use console
use corewin
'------------------------------------------------------------------------------
'GetFields function from modified OxygenBasic\examples\DataProcessing\GetFields.o2bas app
include "GetFields.o2bas"
'------------------------------------------------------------------------------
Type AddressType
FirstName As zstring * 32
LastName As zstring * 32
Company As zstring * 64
Address As zstring * 64
City As zstring * 32
County As zstring * 32
State As zstring * 4
Zip As zstring * 12
Phone1 As zstring * 24
Phone2 As zstring * 24
Email As zstring * 64
Url As zstring * 64
End Type
'==============================================================================
Macro LineInput string (s,c)
s = nuls 1024
s = (char*)fgets(s,1024,c)
End Macro
'==============================================================================
Function main() As sys
string sfileIn = "Myus-500tab.csv"
string sfileOut = "Address50.bin"
string sLine
sys fp1,fp2,fields,Count
Dim As AddressType Address
int bytes,where
fp1 = fopen sfileIn,"r"
If not fp1 Then
printl "PROBLEM #1"
waitkey
exit function
End If
fp2 = fopen sfileOut,"w"
If not fp2 Then
printl "PROBLEM #2"
fclose fp1
waitkey
exit function
End If
Do
sLine = LineInput(fp1)
If Len(sLine) = 0 Then
fclose(fp1)
fclose(fp2)
printl "PROBLEM #3"
waitkey
exit function
End If
Count++
If Count > 50 Then
break
End If
fields = GetFields(sLine,chr(9))
With Address.
FirstName = arg(1)
LastName = arg(2)
Company = arg(3)
Address = arg(4)
City = arg(5)
County = arg(6)
State = arg(7)
Zip = arg(8)
Phone1 = arg(9)
Phone2 = arg(10)
Email = arg(11)
Url = arg(12)
End With
fwrite(@Address,sizeof(Address),1,fp2)
loop
fclose fp1
fclose fp2
printl "Is it done?"
Printl "Now lets read the first record from the binary file" cr "press any key"
waitkey
fp1 = fopen sfileOut,"r+"
If not fp1 Then
printl "PROBLEM #4"
waitkey
exit function
End If
fread @Address,sizeof(Address),1,fp1
Printl "First Name -> " + Address.FirstName
Printl "Last Name -> " + Address.LastName
Printl "State -> " + Address.State
Printl ""
Printl "Now lets read the last record" + cr + "any key to continue"
waitkey
where = 49 * sizeof(Address)
fseek fp1,where,0
fread @Address,sizeof(Address),1,fp1
fread @Address,sizeof(Address),1,fp1
Printl "First Name -> " + Address.FirstName
Printl "Last Name -> " + Address.LastName
Printl "State -> " + Address.State
fclose fp1
Printl cr "any key to continue"
waitkey
End Function
main