Board Logo
« Saving variable values to a file? »

Welcome Guest. Please Login or Register.
Oct 17th, 2017, 3:16pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Saving variable values to a file?  (Read 317 times)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1180
xx Re: Saving variable values to a file?
« Reply #3 on: Aug 12th, 2017, 7:29pm »

Do you have a specific problem in mind? some code started?

You might be talking about saving records eg name, address, phone... or some such repeated data group for several people in one file?

You can also use a variable name for the file and save to how many files you need to save to, by number, then open them by reversing the process.
« Last Edit: Aug 12th, 2017, 7:40pm by bplus » User IP Logged

B+
tooanalytical
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1739
xx Re: Saving variable values to a file?
« Reply #4 on: Aug 12th, 2017, 10:18pm »

on Aug 12th, 2017, 7:29pm, bplus wrote:
Do you have a specific problem in mind? some code started?

You might be talking about saving records eg name, address, phone... or some such repeated data group for several people in one file?

You can also use a variable name for the file and save to how many files you need to save to, by number, then open them by reversing the process.


I have no prepared code or example to show, but you can check the file when the program is first opened, read the file, load into the necessary variables, and then use them in the program wherever necessary.
User IP Logged

stumpy
Full Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 145
xx Re: Saving variable values to a file?
« Reply #5 on: Aug 13th, 2017, 04:43am »

Here is part of what I recently learned about setting up a folder on disc, ready to save some variables in a data file, as tooanalytical has described.

If it might help, I would try to add some more stages of a way to save and load variables, or strings, to and from that folder...

[edit:] as corrected by tsh73, see later.
Code:
'first part of saving and loading strings 

'1. check if the folders exist, or create new folders e.g. "basic" and "newrecord"
'    in e.g. "c:\basic\"

notice "The path will be set to: 'c:\basic\newrecord'"

saveload1$ = "c:\basic" 'defines the first part of the path
saveload2$ = "c:\basic\newrecord" 'defines the second part

'(or: get user to enter a path here instead, and don't define it.
' or: there is also a "relative path" method, which was hard to grasp!)

'-----------------------------------------

g$ = "Save the record to " + saveload2$ + "using two stages:-" + chr$(13) + chr$(13)
g$ = "if the folders 'basic' and 'newrecord' do not exist they will be created " + chr$(13) + chr$(13)
g$ = g$ + "and later (when the save code has been written) " + chr$(13) + chr$(13)
g$ = g$ + "any existing record with the same file name would be overwritten." + chr$(13) + chr$(13)
g$ = g$ + "Ok to proceed?"
confirm g$; answer$

if answer$ = "no" then [quit]

'---- test if the path saveload1$ already exists, or create it

result1 = mkdir(saveload1$)

'---- test if the path saveload2$ already exists, or create it
result2 = mkdir(saveload2$)

g$ = "mkdir" + chr$(13) + "result1 = " + str$(result1) + ", and result2 = " + str$(result2) +chr$(13) + chr$(13)
g$ = g$ + " - the result of 'mkdir()' would only be 0 if the paths did not exist," + chr$(13) + chr$(13)
g$ = g$ + " - when new files would have been created"
notice g$

if result1 = 0 then  notice "'" + saveload1$ + "'" + " has been created"

if result2 = 0 then  notice "'" + saveload2$ + "'" + " has been created"

goto [carryon]

If result1 <> 0 then notice "the paths'" + saveload1$ + " and " + saveload2$ + "already existed"

If result2 <> 0 then notice "the paths'" + saveload2$ + "already existed"

'--------------------------------------------
[carryon]
'etc etc.....

[quit]

end
 

« Last Edit: Aug 14th, 2017, 3:37pm by stumpy » User IP Logged

stumpy
Full Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 145
xx Re: Saving variable values to a file?
« Reply #6 on: Aug 13th, 2017, 12:51pm »

Anyway here's an attempt at part 2

(corrections and improvements would be welcome)

[edit:] notices have been tidied up, see post below from tsh73
Code:
'save-load records to-from a data file - part 2

'PLEASE FIRST MAXIMISE THE PROGRAM's 'Windows window'
'-----------------------------------------

'create, or make sure, that there are such arrays, to contain the strings to be saved and reloaded
dim name$(10)
dim no$(10)

'------------------------------------------

'put 10 numbers in no$(1 to 10), and 10 names in name$(1 to 10)by reading them from [datalist]
restore [datalist]

for n = 1 to 10
   read d$
   no$(n)= d$
   read d$
   name$(n) = d$
next n

'Help says: "DATA is local to the subroutine or function in which it is defined".
[datalist]
data "1", "angela", "2", "bob", "3", "catherine", "4", "donald", "5", "ethel", "6", "fred", "7", "gillian", "8", "harry", "9", "jane", "10", "keith"
data "end"
'--------------------------
' show the data in the program's page

for n= 1 to 10
 print no$(n) + " " + name$(n)
next n

notice "Above, are sets of numbers and names in some strings, shown as a list - click 'ok' to proceed and save them to disc"

cls

'settingsfile$ defines the directory to be addressed
 settingsfile$ = "c:\basic\newrecord\record1.dat"

' Open a data file in the pre-existing folder 'newrecord'
' Create a file 'record1.dat' by putting a blank in it, and close the file
  open settingsfile$ for append as #stringsave
  print #stringsave, ""
  close #stringsave

  'save the data file for 10 names and 10 numbers (as strings) in file 'record1'
  open settingsfile$ for output as #stringsave

  for n= 1 to 10
   print #stringsave, no$(n)
   print #stringsave, name$(n)
  next n

  close #stringsave

'-----------------------------------------------
    dim no$(10) 'make sure that no$() is cleared out
    dim name$(10) 'make sure that name$() is cleared out

g$ = "saved" + chr$(13) + chr$(13)
g$ = g$ + "The numbers and names have been saved" + chr$(13) + chr$(13)
g$ = g$ + "in folder 'newrecord1'" + chr$(13) + chr$(13)
g$ = g$ + "as file 'record1.dat'" + chr$(13) + chr$(13)+ "'ok' to reload them"
g$ = g$ + " into the number and name strings" + chr$(13)
g$ = g$ + "(which have just been cleared by 'dimming' the arrays again)?" + chr$(13) + chr$(13)
g$ = "Click 'ok' to reload the strings"
notice g$


'---------------------------------------------------
    settingsfile$ = "c:\basic\newrecord\record1.dat"
    open settingsfile$ for input as #stringload
    for n= 1 to 10
     line input #stringload, no$(n)
     line input #stringload, name$(n)
    next n

    close #stringload

g$ = "reload" + chr$(13) + chr$(13)
g$= g$ + "The strings have been re-loaded from disc" + chr$(13) + chr$(13)
g$ = g$ + "Click 'ok', to show the strings by re-printing the sets of numbers and names:" + chr$(13) + chr$(13)
notice g$

  for n= 1 to 10
   print no$(n) + " " + name$(n)
  next n

wait

end
 
« Last Edit: Aug 16th, 2017, 02:25am by stumpy » User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3558
xx Re: Saving variable values to a file?
« Reply #7 on: Aug 13th, 2017, 3:42pm »

Hello stumpy
couple of things.
1) MKDIR creates only one directory
So if you want
Code:
C:\basic\newrecord 

you should create
Code:
C:\basic 

then
Code:
C:\basic\newrecord 

2) It somewhat doesn't look right to have a message box saying "OK to do something" and have only one button offered (OK that is).
Message imply I have a choice? Where is it?
May be that's just me.
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
stumpy
Full Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 145
xx Re: Saving variable values to a file?
« Reply #8 on: Aug 13th, 2017, 10:59pm »

tsh73, thanks.

Re.1
That's interesting. Think I must be confused about the meaning of 'directory' but have just tried the following again, on its own -

saveload$ = "c:\basic\newrecord"
result = mkdir(saveload$)
print "result = "; result

In practice these lines do seem to print "0", I think to show the error code "0" (being that for a file not found) and, perhaps on that basis, it also seems to 'create' the new folder 'newrecord', as was needed in part1 (before part2 had been written).
In part 2 the new data file was put into the pre-existing folder 'newrecord' (if the user had been asked to specify it or, as written, if mkdir had created it after looking for it and not finding it). Otherwise, if it had been missing, the program would stop with "folder not found"

So are you pointing out the following?

In:-
[if result = 0 then
notice + "saveload$" + " has been created"]

- that the notice is incorrect, in that saveload$ has not been "created"? i.e. instead it is the folder 'newrecord' which was created, not saveload$, which is the path/(directory)?

but perhaps there is no such a thing as a path? and directory can either mean the tree structure to which reference is made, or mean just a folder?

in wikipedia:
"in computing, a directory is a file system cataloging structure which contains references to other computer files, and possibly other directories. On many computers, directories are known as folders, or drawers to provide some relevancy to a workbench or the traditional office file cabinet. Files are organized by storing related files in the same directory. In a hierarchical filesystem"

The bit in italics seems to explain the confusion between use of the word 'in computing' (being generally correct?) in contrast to its use 'on many computers' (being in specific cases?)

Re.2
Right, that was sloppy. Notice was used, instead of Confirm, just to get a pause of execution while being able to explain the stage in the program, without having to offer any choices but with just 'ok' to carry on grin
« Last Edit: Aug 14th, 2017, 01:50am by stumpy » User IP Logged

stumpy
Full Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 145
xx Re: Saving variable values to a file?
« Reply #9 on: Aug 14th, 2017, 12:31am »

Hello again tsh73,

PS
Re. Re.1
Aha: if the folder basic had not existed (as happened to be the case, but might not have been) mkdir would need to be done to create folder 'basic' first, before then being able to create another folder 'newrecord' in folder 'basic'.

Just as you said, thanks for that and I have tried to correct part1.
Have also tidied up the notices in part2, hope they make more sense.
« Last Edit: Aug 14th, 2017, 02:20am by stumpy » User IP Logged

mlnjvnny
New Member
Image


member is offline

Avatar




PM


Posts: 4
xx Re: Saving variable values to a file?
« Reply #10 on: Aug 15th, 2017, 10:29pm »

I was thinking more on the line of making multiple files with different values for the same variables that you could load from at will. With these examples you guys have been kindly giving me, I think I have an idea of how to open saved files and load values from them, but how do you save them? How do you tell the program what to save?

For a quick example, think of a program that takes contact information and saves it for a contact list, and every contact is saved to its own file. How would you go about saving all the information you input to its own file, one for each and every contact you add? As in, saving information without overwriting or appending, but rather making an entirely different file seperate from the last you used?

(This is just a quick example. I'm not saying this is what I'm planning to use this on since I know there are far better ways to make a contact list than this grin )
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1180
xx Re: Saving variable values to a file?
« Reply #11 on: Aug 15th, 2017, 11:08pm »

When you open a file for OUTPUT you are writing to it, saving the data.

Here open a new file for every contact name you have and save their address and email, just for an example:

Code:
[doAnother]
    print "The contact name will be used as a file name with .txt added on end."
    input "Enter the name or enter nothing to quit ";name$
    if name$ = "" then end
    input "Enter address > ";address$
    input "Enter email > ";email$
    open name$;".txt" for output as #1  'this will open a file for every name (dont repeat names)
    print #1, address$
    print #1, email$
    close #1
    print name$;".txt has been saved."
    print
goto [doAnother]
 


You can also add the path to the folder for the file names:
path$ + "\" + name$ + ".txt"
User IP Logged

B+
mlnjvnny
New Member
Image


member is offline

Avatar




PM


Posts: 4
xx Re: Saving variable values to a file?
« Reply #12 on: Aug 16th, 2017, 12:28am »

on Aug 15th, 2017, 11:08pm, bplus wrote:
When you open a file for OUTPUT you are writing to it, saving the data.

Here open a new file for every contact name you have and save their address and email, just for an example:

You can also add the path to the folder for the file names:
path$ + "\" + name$ + ".txt"


I hadn't thought about using a variable to set a file name. That actually helps a lot. Thanks for the idea.
User IP Logged

stumpy
Full Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 145
xx Re: Saving variable values to a file?
« Reply #13 on: Aug 16th, 2017, 01:43am »

Yes 'path$' is equivalent to, but much better than, 'settingsfile$' grin

on Aug 15th, 2017, 10:29pm, mlnjvnny wrote:
I was thinking more on the line of making multiple files with different values for the same variables that you could load from at will...

Re. different values
The file 'record1' could contain strings to represent the same variables in the strings: no$(), or in name$() as below, or indeed in anythingelse$().
As to the values in the strings, in the example, that depends upon the 'restored' lines of data which are specified by the code to put things into the strings.

In the example the code for 'restore [line]' and the line of data
is just to illustrate one way of loading strings, but clearly the program could do it on the fly e.g. with no$ = str$(201) if a variable 'n' had become 201 as it ran....


re. multiple files
also as below, settingsfile$ (aka 'path$) could instead specify the files '\record2.dat', or '\record3.dat', or '\record4' to contain either the same sort of things in different versions, or yet other things, or sets of things

...settingsfile$ (path$) = "c:\basic\newrecord\record1.dat"
open settingsfile$ for input as #stringload
for n= 1 to 10
line input #stringload, no$(n)
line input #stringload, name$(n)
next n
close #stringload.....


Quote:
...I think I have an idea of how to open saved files and load values from them, but how do you save them?...

settingsfile$ specifies what to save and where to save them, so the code can do the job of saving them.

...settingsfile$ = "c:\basic\newrecord\record1.dat"...

i.e. what to save = record1.dat (with name$'s and no$'s in it)
....where to save record1.dat = c:\basic\newrecord\

saving them:-

...'save the data file for 10 names and 10 numbers (as strings) in file 'record1'
open settingsfile$ for output as #stringsave

for n= 1 to 10
print #stringsave, no$(n)
print #stringsave, name$(n)
next n

close #stringsave...


Quote:
...How do you tell the program what to save?...

- so the code tells what to save in the form of 'no$(n)' or 'name$(n)' or 'somethingelse$', and the files to save it in as 'record1.dat', or 'record2.dat', or 'records3.dat', and the 'restore(d) data' lines of data would contain the details of 'what to save':-

...line input #stringload, no$(n)
line input #stringload, name$(n)...
« Last Edit: Aug 16th, 2017, 02:25am by stumpy » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls