Author Topic: Reading file in Binary mode  (Read 1190 times)

0 Members and 1 Guest are viewing this topic.

Karen Zibowski

  • Guest
Reading file in Binary mode
« on: February 28, 2018, 08:26:24 AM »
Hi

I'm trying to convert a PowerBasic program to an Oxygen
but encounter problems such as

 1.   Equivalent of  ISFILE function in Oxygen  which checks whether a file exist or not

 2.  Equivalent of FREEFILE in Oxygen , when you need to open a file channel

3.  How to open a file in binary mode

The following is my code which cannot be compiled

Code: [Select]
' Read_Filestr.o2bas
$ filename "Read_Filestr.exe"
use rtl64

'===================================
FUNCTION readFileStr(gfnam AS STRING) AS STRING
  ' reads in the entire file as a  string
   Long ff
  string bst

 '   IF ISFALSE ISFILE(gfnam) THEN
 '      File not found error  -- we return a null string
    '    FUNCTION = ""
    '    EXIT FUNCTION
  ' END IF


   ff =  3 'FREEFILE
   OPEN gfnam FOR BINARY AS #ff
   GET$ #ff, LOF(ff), bst
   CLOSE #ff
  FUNCTION = bst
END  FUNCTION


                         
'===========================================
'  Start of main
 string filnam ,  wfilstr

 filnam = "co2.exe"
 
wfilstr = readFileStr(filnam)

print " wanted file string : " + $CRLF + wfilstr






Regards
Karen

Charles Pegge

  • Guest
Re: Reading file in Binary mode
« Reply #1 on: February 28, 2018, 10:34:27 AM »

Channelled File i/o is something we need to develop for PB compatibility. But where the files are not huge, and LAN file transactions are not required, I would recommend using our core GetFile and PutFile. they work directly with strings, binary content or otherwise.

Code: [Select]
string s=GetFile "MyfileA.bin"
PutFile s,"MyFileB.bin"

If the file does not exist, your string will be empty.

Karen Zibowski

  • Guest
Re: Reading file in Binary mode
« Reply #2 on: February 28, 2018, 11:24:47 AM »
Many thanks Charles,  I have modified my program to

Code: [Select]
' Read_Filestr.o2bas
$ filename "Read_Filestr.exe"
use rtl64

'===================================
FUNCTION readFileStr(gfnam AS STRING) AS STRING
  ' reads in the entire file as a  string
   Long ff
   string bst

 '   IF ISFALSE ISFILE(gfnam) THEN
 '      File not found error  -- we return a null string
    '    FUNCTION = ""
    '    EXIT FUNCTION
  ' END IF
   

 '  ff =  3 'FREEFILE
 '  OPEN gfnam FOR BINARY AS #ff
 '  GET$ #ff, LOF(ff), bst
  ' CLOSE #ff
    bst = GetFile gfnam
 

  FUNCTION = bst
END  FUNCTION


                         
'===========================================
'  Start of main
  string filnam
  string wfilstr
 

 filnam = "C:\oxygenBasic Compiler\bmt.dll"

 
  wfilstr = readFileStr(filnam)

print " wanted file string : "  + wfilstr

print " length of file string : " + str$(len(wfilstr))




Kindly check if I'm doing it right

One more thing what is oxygen equivalent to PowerBasic    $CRLF     ( carriage feed character)
I was not able to write the code as

Code: [Select]
print " wanted file string : "  +  $CRLF + wfilstr




Charles Pegge

  • Guest
Re: Reading file in Binary mode
« Reply #3 on: February 28, 2018, 02:17:37 PM »
Yes, that looks fine Karen, and it worked on my system (with a different file)

to make your own crlf equate:

Code: [Select]
$ crlf chr(13,10)
'same as:
% crlf chr(13,10)

once defined, it is referred to as crlf, dollars prefixes are not used

Karen Zibowski

  • Guest
Re: Reading file in Binary mode
« Reply #4 on: February 28, 2018, 09:06:26 PM »
Thanks Charles