Mintoris Forum

Author Topic: Storing variable value for next time  (Read 2715 times)

Bitrate

  • Jr. Member
  • **
  • Posts: 5
Storing variable value for next time
« on: Dec 16, 2011, 03:35 PM »
I wish to store the final value of a numeric variable so next time I run my basic program, I will start from that value.
How should I do get this done.
Thank you very much for the tips.
Chris.  ::)



Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Storing variable value for next time
« Reply #1 on: Dec 16, 2011, 07:23 PM »
To store a value to a file use the following code:

Open 1, "filename.dat", "w"
Writeln 1, str$(value)
Close 1

You can read the value back with this code:

Open 1, "filename.dat", "r"
Readln 1, value$
Close 1
value = val(value$)


You can use the IsFile() function to see if your data file exists.

If IsFile("filename.dat") Then
  Open 1, "filename.dat", "r"
  Readln 1, value$
  Close 1
  value = val(value$)
Else
  value = 0
Endif



If you have more than one value just read and write them in order:

Open 1, "filename.dat", "w"
Writeln 1, str$(value1)
Writeln 1, str$(value2)
Writeln 1, str$(value3)
Close 1
« Last Edit: Dec 16, 2011, 07:31 PM by Chuck »

Qrandom

  • Jr. Member
  • **
  • Posts: 4
Re: Storing variable value for next time
« Reply #2 on: Dec 21, 2017, 03:52 AM »
how about if you want to store and retrieve whole blocks of binary data ie read and write actual 8 bit bytes to a file and then read them back (and write them) as binary data, say you have a file that is 57 bytes (that each byte can be any value 8 bits can have) you want to be able to read and write as compactly as possible?

Chuck

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1899
Re: Storing variable value for next time
« Reply #3 on: Jan 19, 2018, 02:32 AM »
how about if you want to store and retrieve whole blocks of binary data ie read and write actual 8 bit bytes to a file and then read them back (and write them) as binary data, say you have a file that is 57 bytes (that each byte can be any value 8 bits can have) you want to be able to read and write as compactly as possible?

You want the ReadBytes n, data$ {, maxBytes} and WriteBytes n, data$ commands.

Version 8 will introduce Byte variables and Byte arrays which will make this process even easier.