• Welcome to Jose's Read Only Forum 2023.
 

how to store a string into a binary file

Started by Chris Chancellor, November 28, 2018, 08:47:20 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello Charles

in PB we can store a string  (as much as 4MB ) into a binary file,  how do we do this in O2 ?

see the PB program below

#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG

    LOCAL myfileSt AS STRING
    LOCAL FileNumber AS LONG

      myfileSt = " this is my file string and needs to be stored in a binary file all at one go "+ $CRLF  + _
                  " as much stuff as possible that you might want to " + $CRLF + _
                  " ya da ya da ya da "

          ' create a binary file to store in the file string
        FileNumber = FREEFILE
        OPEN "Testfile.txt" FOR BINARY AS FileNumber BASE = 0

        PUT FileNumber,, myfileSt
        CLOSE FileNumber

       ?  " ok "

END FUNCTION

Brian Alvarez

I dont see any native commands for this, so i am guessing you have to use API functions.
Check out OpenFile, CloseFile, etc.

https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-openfile

Chris Chancellor

Thanxx Brian

i have figured it out from my previous notes and Charles

here's the equivalent code  which is much shorter than PB


' Sav_BinFile.o2bas

$filename "Sav_BinFile.exe"
use rtl64

use corewin


string  myfileSt
 

      myfileSt = " this is my file string and needs to be stored in a binary file all at one go "+ chr(13,10)  + _
                  " as much stuff as possible that you might want to " + chr(13,10) + _
                  " ya da ya da ya da "       


     putfile "Testfile.txt",myfileSt

    print " ok "


Chris Chancellor

yes,  O2 is very good ,  i have tested it on a 4.27MB file and was able to read it into a string and
save it as a binary file under new name.

the new file is also 4.27MB !


' Sav_BinFile.o2bas
'  program to save an entire string into file

$filename "Sav_BinFile.exe"
use rtl64

use corewin


string  myfileSt
 

      myfileSt = " this is my file string and needs to be stored in a binary file all at one go "+ chr(13,10)  + _
                  " as much stuff as possible that you might want to " + chr(13,10) + _
                  " ya da ya da ya da "       


     Putfile "Testfile.txt",myfileSt

     print " ok  created the Testfile.txt"


'-------------------------------------------------------------------------------
  ' Now we read a 4.27 MB file and save it as another file
'   called  New shopping mall.txt

       string BigFile_st
     
      BigFile_st  = Getfile "Te Whetu Shopping Mall.txt"

     Putfile "New shopping mall.txt" ,  BigFile_st

     print "  ok  have created a new file called  New shopping mall.txt "