• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Saving a string into binary file and reading a file into a string

Started by Chris Chancellor, November 28, 2018, 11:20:28 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Chris Chancellor

Hello All

This program shows  how flexible O2 is

        its command   PutFile  filename , stringname

               Putfile "Testfile.txt", myfileSt    will
               will   save a string myfileSt  into a binary file called Testfile.txt

           while           filestring    =     GetFile  filename
               will read the file and set the entire file data into a string



' Sav_BinFile.o2bas
'  Program to save an entire string into a binary file.
'  And to read another big file into a string then
'  save this string into a new file

$filename "Sav_BinFile.exe"
use rtl64

use corewin


'----------------------------------------------------
'  To save a string into a Binary file
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 "Te Whetu Shopping Mall.txt"   into a string
'   and then 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 "





Chris Chancellor


Time to start converting your 32bits PB programs to 64bits O2


Charles Pegge

You can also save dynamic arrays in a similar way.

Every array defined with redim, has a string buffer associated with it, which can be filed.


'FILING DYNAMIC DATA
'
redim single s(100)
redim single u(0)
s(13)=39.5
'
'SAVING DYNAMIC SINGLES
putfile "t.txt",s_buffer
'
'LOADING DYNAMIC SINGLES
u_buffer=getfile "t.txt"
@u=strptr(u_buffer)
print u(13)

Chris Chancellor

Thanxx Charles

that's a fast and efficient way to handle arrays and files


Only problem with translating from PB is the REDIM statement

in PB ,     the REDIM statement does not preserve the data and resets the arrays to zeroes

while the REDIM statement in O2  is like  PB's    REDIM PRESERVE  statement
where arrays are not zeroised and data is preserved


is there a RESET Array statement  in O2?



Charles Pegge

You can clear the dynamic array by redimming to 0 first:

redim single s(0)
redim single s(100)


but be careful with string arrays: Each string should be made empty before redimming

Chris Chancellor

that was a good one


redim single s(0)
redim single s(100)


but it uses 2 lines of codes,   can you please write it as a macro  ( named it as RedimRS )
so that i can called it and run it in one line of code

say    RedimRS  single s(100)

which is equivalent to
redim single s(0)
redim single s(100)


Charles Pegge



def redimreset
==============
redim %1 %2 (0)
redim %1 %2 (%3)
end def

redim single s(100)
s(13)=39.5
redimreset single s(100)
print s(13)


Chris Chancellor

#7
Thanxx a lot Charles

but in your reply #4 you said
Quotebut be careful with string arrays: Each string should be made empty before redimming

so can we use it for String arrays, kind of like below ?   Is this valid ?


RedimReset   String ArrStr(200)



Charles Pegge

Hi Chris,

The problem arises when you repeatedly redim a string array with a lesser number of elements. The GC will tend to accumulate the detached strings and only release them at the end of the program.

Chris Chancellor

Thanxx Charles

is there a way to clear memory and or get the GC  or Garbage Collector to do its job halfway
in the program ? 

after which we can do RedimReset string arrays.

Mike Lobanovsky

Quote from: Charles Pegge on December 04, 2018, 02:36:59 PMThe GC will tend to accumulate the detached strings and only release them at the end of the program.

I think it might be worthwhile to also add a command (or meta) to be able to programmatically force the GC to clean up unused memory at some points in the program flow at the programmer's option.

Most programs have lesser time critical portions (e.g. user interaction inputs that take the CPU aeons to wait, idle time wise, till the user makes a choice) that can be used to tidy up the memory in the mean time.

Many garbage-collecting interpreters (BASIC and Lisp and others) provide such an option alongside automatic garbage collection that cannot be 100% fool proof taking heuristic decisions as to when (lengthy) garbage collection routines are the least likely to interfere with the program's overall performance.
Mike
(3.6GHz Intel Core i5 w/ 16GB RAM, 2 x GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)

Charles Pegge

Hi Mike,

Only o2-native strings have garbage collection lists. So the easiest way to take full control over memory management is to use bstrings, or getmemory/freememory blocks  for generic use. Bstrings will only destroy their original content when reassigned, but the program must eventually release the remainders explicitly.


Chris Chancellor

Hello Charles

you mentioned BString usage,  so that we can use BString arrays and that we can Redimreset them
as many times in the program as we like,  kinda like the below code  ?


RedimReset   BString ArrStr(200)


Can we do this?   rather than using String we use BString

Charles Pegge

Hi Chris,

I've included a stronger redim in OXSC181205. When used with the qualifier clear, it nullifies all the elements.

For strings and UDTs, It also prevents reduction of the dynamic array. So strings etc. get recycled and do not end up in limbo.

https://github.com/Charles-Pegge/OxygenBasic


'17:54 04/12/2018
'SAFER REDIM RESET FOR STRINGS ETC
'
redim string s(100)
s(13)="ok"
redim string s(100)
print s(13) ' ok
redim string s(20) clear
print s(13) ' [empty]
print len s_buffer '400 (100 elements)


Chris Chancellor

Thanxx a lot Charles

i will check it out when your self compile O2 arrives