Oxygen Basic

Programming => Bugs & Feature Requests => Topic started by: jcfuller on March 12, 2018, 04:19:32 AM

Title: File IO
Post by: jcfuller on March 12, 2018, 04:19:32 AM
Charles,
  What are the plans for file IO similar to FreeBasic?
I have experimented a bit using WinApi but it would be more familiar using the msvcrt functions.

James
Title: Re: File IO
Post by: Charles Pegge on March 12, 2018, 05:12:58 AM
Hi James,

I'm going to include MSVCRT in the CoreWin headers. We can put basic wrappers around fopen/fread/fclose, if necessary.
Title: Re: File IO
Post by: jcfuller on March 12, 2018, 10:21:09 AM
Charles,
  Now that we have msvcrt.inc how about a short demo on file IO.
my initial attempt failed miserably:)
James
Title: Re: File IO
Post by: Charles Pegge on March 12, 2018, 11:20:12 AM
James,

You can use a string as a buffer instead of the more primitive char array.

Code: [Select]
'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
Title: Re: File IO
Post by: jcfuller on March 12, 2018, 11:55:48 AM
Charles,
rtl32 only??

James
Title: Re: File IO
Post by: Charles Pegge on March 12, 2018, 01:01:12 PM
Hi James,

One essential missing from msvcrt.inc:

#ifndef mode64bit
  extern lib "Msvcrt.dll" cdecl
#else
  extern lib "Msvcrt.dll"
#endif

Title: Re: File IO / Random Access / Records
Post by: Charles Pegge on March 13, 2018, 02:25:51 AM
Chris,

Note hf is the file handle (a pointer), and fd is the file descriptor index, which you may be more accustomed with.

Code: [Select]
'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
Title: Re: File IO
Post by: jcfuller on March 13, 2018, 04:23:10 AM
Charles,
  Excellent. I really don't need (basic) wrappers as I am familiar with the c notation or I can always write what I need  with bc9 and let it do the translation for OxygenBasic. :)

James

Title: Re: File IO
Post by: Charles Pegge on March 13, 2018, 05:26:58 AM
Yes, this API is really simple to use, with no special syntax to memorise. I think the excessive wordiness of Qbasic in file i/o and function headers was a serious design flaw in the first place.
Title: Re: File IO
Post by: Charles Pegge on March 14, 2018, 04:22:59 AM
Reading a complete file

loads the contents of any type of file into a string

Code: [Select]
'/*
'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
Title: Re: File IO
Post by: Mike Lobanovsky on March 14, 2018, 04:34:26 AM
... I can always write what I need  with bc9 and let it do the translation for OxygenBasic. :)

 :o

JAMES!!!

WHERE CAN I GET THAT WONDER OF YOURS THAT CAN READ/WRITE O2 CODE?!

MOST IMPORTANTLY, CAN YOU ALSO READ FREEBASIC CODE AND GENERATE GCC/VC OUTPUTS?!
Title: Re: File IO
Post by: jcfuller on March 14, 2018, 06:34:41 AM
Mike,
  I was referring to file IO only.
bc9

OPEN "text.txt" FOR INPUT As FP1

translates to

FP1 = fopen("text.txt","r")


James
Title: Re: File IO
Post by: Mike Lobanovsky on March 14, 2018, 11:40:40 AM
Oh, I see.

Have you ever tried to retarget the translator for anything but BCX?
Title: Re: File IO
Post by: jcfuller on March 14, 2018, 12:22:52 PM
Oh, I see.

Have you ever tried to retarget the translator for anything but BCX?
Mike,
  No. I really like c++ just not its nomenclature, and with the advent of Fred Harris's TCLib (and my tweaks) I can create very small exe's too.

James
Title: Re: File IO
Post by: Mike Lobanovsky on March 14, 2018, 12:57:02 PM
James,

No no, I mean the other side of the pipe -- its input. Any other BASIC dialect in place of BCX? (that's not for PB but another useful purpose)
Title: Re: File IO
Post by: Patrice Terrier on March 14, 2018, 02:14:13 PM
FOpen the basic version keyword was first introduced by Ethan Winer (Crescent Software) in 1991.
It was modeled closely onto C, and since that time i have always used it, even in PB and now in C++
same for FPut, FGet, FClose, etc.
I found this syntax much easier than the verbose PB's 
OPEN "text.txt" FOR INPUT As FP1

;)

Title: Re: File IO
Post by: Charles Pegge on March 15, 2018, 05:28:33 AM

One of the flaws in Basic has been to conflate verbosity with user-friendliness. If we can trim off these embellishments and,  instead use regular syntax, Basic will be a fitter language :)
Title: Re: File IO
Post by: JRS on March 15, 2018, 08:00:05 PM
Great to see you back on the OxygenBasic forum again Patrice!

Quote
I found this syntax much easier than the verbose PB's
OPEN "text.txt" FOR INPUT As FP1

It doesn't get much simpler than this.

Code: Script BASIC
  1. filestr = LoadString("text.txt")
  2.  
Title: Re: File IO
Post by: Patrice Terrier on March 15, 2018, 11:22:15 PM
My point is, as long as there is an existing API, why not using the same name than the original, there is no need to re-invent the wheel or using a convoluted syntax to encapsulate the core function.

FOpen has been there for me, since the PDS time...
Title: Re: File IO
Post by: Mike Lobanovsky on March 16, 2018, 12:46:40 AM
Patrice,

LoadString() is an existing API. It comes OOTB right from Windows SDK rather than C/C++ RTL. :)
Title: Re: File IO
Post by: Patrice Terrier on March 16, 2018, 05:00:39 AM
LoadString and FOpen are two distincts animals that do not serve the same purpose.
Title: Re: File IO
Post by: JRS on March 16, 2018, 05:06:20 AM
True but we were discussing verbose.
Title: Re: File IO
Post by: Charles Pegge on March 16, 2018, 06:32:01 AM
Loading and saving whole files are essentially 3-step processes, but I think it's still useful to have them as one-liners, since this will meet the majority of file i/o needs.

Code: [Select]
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