Board Logo
« Saving variable values to a file? »

Welcome Guest. Please Login or Register.
Nov 24th, 2017, 6:20pm


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 424 times)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1213
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: 5
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