IonicWind Software

IWBasic => General Questions => Topic started by: CodeTRUCKER on December 31, 2007, 12:00:23 AM

Title: Storing values
Post by: CodeTRUCKER on December 31, 2007, 12:00:23 AM

Hello,

I am attempting my first app in EBASIC and I need to know how to write a list of variable values to a simple text file to use as an initialization on opening my app.  I have tried to follow the Help Files, but I'm not getting it. 

What syntax would I use if my file was, say "C:\variables.txt"  I only need simple ASCII strings as I can convert types Ok.  I also can handle a For/Next with an array to load and unload.  Any help is appreciated.

Thanks,
Calvin
Title: Re: Storing values
Post by: GWS on December 31, 2007, 02:46:12 AM
Hi Calvin,

Here's a quick attempt at it .. you can probably improve it for your own use ..  :)


openconsole

def valid,dir,exists:int
def datafile:file
def values[10],workfile,filename,a$,b$,c$:string

cls

' check for the Previous Results data file ...
' if it doesn't exist, create it ...

workfile = "data.txt"
exists = 0: ' assume the file does not exist
' try to find a .txt file in the home directory ..
dir = FINDOPEN(GETSTARTPATH +"*.txt")
IF(dir)
  DO
      filename = lcase$(FINDNEXT(dir))
      if (filename = workfile) then exists = 1
  UNTIL filename = ""
  FINDCLOSE dir
ENDIF

if (exists)
' **** EXISTING SYSTEM ****
'
if(OPENFILE(datafile,GETSTARTPATH + "data.txt","A") = 0)
' read the previously stored values ...
for i = 0 to 9
      read datafile, values[i]
Print values[i]
next i
else
' the read failed for some reason ..
a$ = "Data file could not be read." + chr$(10)
b$ = "Delete the file data.txt and re-start."
c$ = a$ + b$
MESSAGEBOX(w1,c$,"File Read Error",0|0x30)
endif
' check for no data yet saved ..
if (left$(values[0],2) <> "00")
' previous data available ..
valid = 1
else
' no data yet available ..
valid = 0
endif
else
' *** NEW SYSTEM ****

' we need to create a new Results file ...
if(OPENFILE(datafile,GETSTARTPATH + "data.txt","W") = 0)
' initialise values array values[ ]...
' create default or required values ..
values[0] = "1","2","3","4","5","6","7","8","9"   

for i = 0 to 9
Write datafile, values[i]
next i

    a$ = "A data file 'data.txt' has been created."
    Print a$
else
a$ = "data.txt file could not be created." + chr$(10)
b$ = "Check files in the program folder are not read-only."
c$ = a$ + b$
Print c$
endif

endif


do:until inkey$<>""
closeconsole
end



The first run will not find a data file, so it will create one.
Next time you run it, the file will be found and the values are printed out.


best wishes, :)

Graham
Title: Re: Storing values
Post by: pistol350 on December 31, 2007, 05:44:27 AM
Hi graham!

This code is rather for Cbasic  ;)
By the way, there is someting that i don't get.
In line, (let's say) 27 , we have this :

if(OPENFILE(datafile,GETSTARTPATH + "data.txt","A") = 0)

The parameter to read data here is "A" ,i thought it had to be "R",
but the initial code compiles and runs fine under Cbasic. Any explanation on this ?

Regards,
Peter
Title: Re: Storing values
Post by: LarryMc on December 31, 2007, 06:58:20 AM
Looks like a typo to me. "A" is for append, "W" is for write (which overwrites the file if it exists) and "R" is for read, and "R+" for read/write on an existing file.

Larry
Title: Re: Storing values
Post by: GWS on December 31, 2007, 07:11:59 AM
QuoteThis code is rather for Cbasic 

Yes it is ..  :) ..I don't have EBasic loaded at the moment, but I don't think there is much difference as far as console work is concerned.

For a quick reply, I cobbled the example together from other code I just happened to have here.  That part with the "A" specification was from a program that needed to add more data to the file.  So you are right, for this example "R" is more correct  :)

I also forgot to close the file:

Closefile datafile

at the end of the program. ::)

best wishes, :)

Graham

Title: Re: Storing values
Post by: CodeTRUCKER on December 31, 2007, 08:39:54 AM
Thank you all for the replies!  I will insert this in my code and see what happens. 

Quote from: GWS on December 31, 2007, 07:11:59 AM
QuoteThis code is rather for Cbasic 

Yes it is ..  :) ..I don't have EBasic loaded at the moment, but I don't think there is much difference as far as console work is concerned.


FYI - I am workin on a Windows GUI app.  Is this compatible?
Title: Re: Storing values
Post by: GWS on December 31, 2007, 09:11:07 AM
Ooh - I knew I'd get into trouble giving a quick response ..  ;D

Not exactly Calvin .. the method is appropriate, but the code would need a bit of a change ..

For instance, you can only use 'Print .. message ..' in the way shown, for Console apps.
I usually use 'MessageBox win ... ' to put out information in a Window app.  (In fact I left a 'Messagebox' statement in there by mistake).
You can easily look up the syntax for a messagebox in the EBasic help system.

Instead of 'Openconsole', you need to open a Window and handle the Windows messages, but you are probably proceeding down those lines OK already.

I think the file handling commands will be pretty much the same - it was only intended to give you a few quick ideas of one way of doing it. :)

Pick the bits out that fit your program requirement and watch out for any small syntax differences in EBasic.

I can't knock up an EB example quickly 'cos I'm not set up for it at the moment ..

all the best, :)

Graham

Title: Re: Storing values
Post by: CodeTRUCKER on December 31, 2007, 09:40:04 AM
Thanks, Graham.  I'll give it a whirl and see what I can hash out.

Calvin