Author Topic: File IO  (Read 5052 times)

0 Members and 3 Guests are viewing this topic.

jcfuller

  • Guest
File IO
« 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

Charles Pegge

  • Guest
Re: File IO
« Reply #1 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.

jcfuller

  • Guest
Re: File IO
« Reply #2 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

Charles Pegge

  • Guest
Re: File IO
« Reply #3 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

jcfuller

  • Guest
Re: File IO
« Reply #4 on: March 12, 2018, 11:55:48 AM »
Charles,
rtl32 only??

James

Charles Pegge

  • Guest
Re: File IO
« Reply #5 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


Charles Pegge

  • Guest
Re: File IO / Random Access / Records
« Reply #6 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
« Last Edit: March 13, 2018, 02:34:13 AM by Charles Pegge »

jcfuller

  • Guest
Re: File IO
« Reply #7 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


Charles Pegge

  • Guest
Re: File IO
« Reply #8 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.

Charles Pegge

  • Guest
Re: File IO
« Reply #9 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

Mike Lobanovsky

  • Guest
Re: File IO
« Reply #10 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?!

jcfuller

  • Guest
Re: File IO
« Reply #11 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
« Last Edit: March 14, 2018, 07:02:55 AM by jcfuller »

Mike Lobanovsky

  • Guest
Re: File IO
« Reply #12 on: March 14, 2018, 11:40:40 AM »
Oh, I see.

Have you ever tried to retarget the translator for anything but BCX?

jcfuller

  • Guest
Re: File IO
« Reply #13 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

Mike Lobanovsky

  • Guest
Re: File IO
« Reply #14 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)