Author Topic: Saving a string into binary file and reading a file into a string  (Read 239 times)

0 Members and 1 Guest are viewing this topic.

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
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


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




Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Re: Saving a string into binary file and reading a file into a string
« Reply #1 on: November 28, 2018, 11:22:17 PM »

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


Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Saving a string into binary file and reading a file into a string
« Reply #2 on: December 03, 2018, 02:43:27 PM »
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.

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

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Re: Saving a string into binary file and reading a file into a string
« Reply #3 on: December 03, 2018, 03:40:56 PM »
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?



Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Saving a string into binary file and reading a file into a string
« Reply #4 on: December 03, 2018, 05:08:28 PM »
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

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Re: Saving a string into binary file and reading a file into a string
« Reply #5 on: December 03, 2018, 05:39:59 PM »
that was a good one

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


Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Saving a string into binary file and reading a file into a string
« Reply #6 on: December 03, 2018, 07:23:50 PM »

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

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Re: Saving a string into binary file and reading a file into a string
« Reply #7 on: December 04, 2018, 02:00:01 PM »
Thanxx a lot Charles

but in your reply #4 you said
Quote
but 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 ?

Code: [Select]
RedimReset   String ArrStr(200)

« Last Edit: December 04, 2018, 02:15:02 PM by Chris Chancellor »

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Saving a string into binary file and reading a file into a string
« Reply #8 on: December 04, 2018, 02:36:59 PM »
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.
« Last Edit: December 04, 2018, 02:42:38 PM by Charles Pegge »

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Re: Saving a string into binary file and reading a file into a string
« Reply #9 on: December 04, 2018, 04:58:15 PM »
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.

Offline Mike Lobanovsky

  • Jr. Member
  • **
  • Posts: 94
Re: Saving a string into binary file and reading a file into a string
« Reply #10 on: December 04, 2018, 09:50:56 PM »
The 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)

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Saving a string into binary file and reading a file into a string
« Reply #11 on: December 04, 2018, 10:47:20 PM »
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.


Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Re: Saving a string into binary file and reading a file into a string
« Reply #12 on: December 05, 2018, 03:12:19 PM »
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  ?

Code: [Select]
RedimReset   BString ArrStr(200)

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

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Saving a string into binary file and reading a file into a string
« Reply #13 on: December 05, 2018, 04:53:38 PM »
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

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

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 269
Re: Saving a string into binary file and reading a file into a string
« Reply #14 on: December 05, 2018, 06:05:58 PM »
Thanxx a lot Charles

i will check it out when your self compile O2 arrives