CAN I MAKE MY PROGRAM WRITE .TXT DOCUMENTS TO STORE VARIABLES???

I recently came across a post from BIG JOHN. This is the post...

"writing BMP images
Hi,
I assume you are using:
CALL writeImage("MS BMP",imagedata$,filename$)
where imagedata$ is the string produced by:
BOX KEEP left,right,bottom,top IN imagedata$
Images can be recovered and shown with:
CALL readImage("MS BMP",imagedata$,filename$)
Big John
By BigJohn at 02/10/2010 - 18:56"

I am trying to make a high score feature in one of my games. I was wondering if you can write BMP image files, is it possible to make my game write and save my "highscore" variable to a .txt document then when I need to use it just simply open and read that documents variable and use that to display all the high scores?

If this is possible please reply to me.

Comments

high scaore variables

Hi,

It is possible to BOX KEEP a portion of your screen, for example a screen display showing a game high score, and to save this to a BMP image file. However the BMP image size is very large. A more convenient way is to save the high score data itself in a text file. Later you can read the data from the text file whenever you need it.

Big John

I dont understand

So can you describe how I do this? I need to know how to write and read data from a .DAT file during my program, while its working. i need to save high scores and read the data to produce a high score page after i win or lose my game.

I FOUND EVIDENCE

I found evidence in a program that someone else made to support the fact that true basic can read info from a .DAT document. here is some source code from that program to read data....

IF CHOICE = 3 THEN
PRINT "LOADING..."
OPEN #1: NAME "EQUIPTMENT.DAT",ACCESS OUTIN, CREATE NEWOLD
DO WHILE MORE #1
INPUT #1: CURRENT_LEVEL
INPUT #1: QWE
INPUT #1: QWR
INPUT #1: QWT
INPUT #1: QWY
INPUT #1: ATTACK_TYPE
INPUT #1: W$
INPUT #1: PASSWORD$
INPUT #1: DEFEAT1
INPUT #1: DEFEAT2
INPUT #1: DEFEAT3
INPUT #1: DEFEAT4
INPUT #1: DEFEAT5
INPUT #1: DEFEAT6
INPUT #1: DEFEAT7
INPUT #1: DEFEAT8
INPUT #1: DEFEAT9
INPUT #1: DEFEAT10
INPUT #1: DEFEAT11
INPUT #1: DEFEAT12
INPUT #1: DEFEAT13
INPUT #1: DEFEAT14
INPUT #1: TEACHER_DEFEAT
INPUT #1: HP
INPUT #1: MP
LOOP
CLOSE #1
CALL LEVEL1
END IF

The file "EQUIPTMENT.DAT" reads as follows when opened with windows notepad...

1.5
1
1
0
0
2
GAT
MOOSEY
1
1
1
1
1
1
1
1
1
1
1
1
1
1
14
400
209

I also belive I have found evidence that it is possible to save data...

IF MENU = 4 THEN
PRINT "SAVING DATA..."
OPEN #1: NAME "EQUIPTMENT.DAT",ACCESS OUTIN, CREATE NEWOLD
ERASE #1
RESET #1: BEGIN
PRINT #1: CURRENT_LEVEL
PRINT #1: QWE
PRINT #1: QWR
PRINT #1: QWT
PRINT #1: QWY
PRINT #1: ATTACK_TYPE
PRINT #1: W$
PRINT #1: PASSWORD$
PRINT #1: DEFEAT1
PRINT #1: DEFEAT2
PRINT #1: DEFEAT3
PRINT #1: DEFEAT4
PRINT #1: DEFEAT5
PRINT #1: DEFEAT6
PRINT #1: DEFEAT7
PRINT #1: DEFEAT8
PRINT #1: DEFEAT9
PRINT #1: DEFEAT10
PRINT #1: DEFEAT11
PRINT #1: DEFEAT12
PRINT #1: DEFEAT13
PRINT #1: DEFEAT14
PRINT #1: TEACHER_DEFEAT
PRINT #1: HP
PRINT #1: MP
CLOSE #1
PAUSE .5
CALL LEVEL1
END IF

So now that I believe that this is possible to accomplish I was hoping that sombody could describe in detail how to save variables to .TXT or in this case .DAT files.

Also how do I make a .DAT file? Is it made like how I make my .BAT files? Where i just save a notepad document as filename.bat and switch the txt document to all files, Except save it as filename.DAT?

Make a .DAT file ...

bubbachunk ... That's easy. RUN your program from the TB Editor COMMAND line, using the command:


run >> filename.dat

Then save the 'filename.dat' to disk memory. Regards ... Tom M

TB program to compute lowpass filter elements

bubba ... Here's a useful program I wrote back in 2003. It computes inductor and capacitor values for a lowpass RF filter. You need to become an electrical engineer to comprehend this program. In my next Forum message will be the data file I created with this program. Regards ... Tom M

! Filename: chebfilt.tru, Tom M, 20030216, tel: aaa-bbb-cccc
print
PRINT " This program computes the normalized elements (g's) for Chebyshev lowpass filters"
PRINT

OPTION BASE 1
OPTION ANGLE radians
LET k3=1
DIM gn(0)

DEF fnlgt(x)=log(x)/log(10)
DEF fnfp(x)=sgn(x)*(abs(x)-int(abs(x)))
DEF fnsinh(x)=.5*(exp(x)-exp(-x))
DEF fncosh(x)=.5*(exp(x)+exp(-x))
DEF fntanh(x)=(exp(2*x)-1)/(exp(2*x)+1)
DEF fncoth(x)=1/fntanh(x)
DEF fnarcsinh(x)=log(x+sqr(x*x+1))
DEF fnarccos(x)=log(x+sqr(x*x-1))
DEF fna(i)=sin(.5*(2*i-1)*pi/n)
DEF fnb(i)=g*g+sin(i*pi/n)*sin(i*pi/n)

PRINT
INPUT prompt " Number of poles (2 to 20) = ": n
MAT redim gn(n)
PRINT
INPUT prompt " Passband ripple (dB) = ": ap
PRINT
INPUT prompt " Source resistance (Ohms) = ": rs
PRINT
INPUT prompt " Cut-off frequency (Hz) = ": bw
PRINT
INPUT prompt " Normalize to 3-dB bandwidth (Y/n) ": r$
PRINT
IF r$="Y" or r$="y" then LET k3=fncosh(fnarccos(1/sqr(10^(.1*ap)-1))/n)

LET beta=log(fncoth(.025*ap/fnlgt(exp(1))))
LET gg=fnsinh(.5*beta/n)
LET gn(1)=2*fna(1)/gg

FOR i=2 to n
LET gn(i)=4*fna(i-1)*fna(i)/fnb(i-1)/gn(i-1)
NEXT i

FOR i=1 to n
IF fnfp(i/2)=0 then
PRINT using " L(###) = ##.###^^^^ Henry (Series) ": i, gn(i)*k3*rs/(2*pi*bw)
ELSE
PRINT using " C(###) = ##.###^^^^ Farad (Shunt) ": i, gn(i)*k3*rs/(2*pi*bw)
END IF
NEXT i
PRINT using " Load resistance = ###,###.# Ohms (Shunt) ": rs*gn(n)/gn(1)
PRINT
! mat print gn;

PRINT
PRINT " Done ... "
END

Data file for the LP filter program

bubba ... Here's the data file I wanted to save to disk memory. I used the following command to create the data file:


run >> chebfilt_20100225.dat

This job was easy! Regards ... Tom M

This program computes the normalized elements (g's) for Chebyshev lowpass filters

Number of poles (2 to 20) = 5

Passband ripple (dB) = .2

Source resistance (Ohms) = 50

Cut-off frequency (Hz) = 1e6

Normalize to 3-dB bandwidth (Y/n) y

C( 1) = 11.716e-06 Farad (Shunt)
L( 2) = 18.901e-06 Henry (Series)
C( 3) = 14.482e-06 Farad (Shunt)
L( 4) = 18.901e-06 Henry (Series)
C( 5) = 11.716e-06 Farad (Shunt)
Load resistance = 50.0 Ohms (Shunt)

Done ...

I DONT UNDERSTAND

So can you describe how I do this? I need to know how to write and read data from a .DAT file during my program, while its working. i need to save high scores and read the data to produce a high score page after i win or lose my game.

I am using TB BRONZE AND TB SILVER

Reading and writing to a text file

Hi,

Here are two sub-routines for reading and writing data to files.

SUB write_file(filename$,array$())
OPEN #4:NAME filename$,create newold, org text, access outin
ERASE #4
SET #4: MARGIN maxnum
LET total=ubound(array$)
FOR n=1 to total
PRINT #4:array$(n)
NEXT N
CLOSE #4
END SUB

SUB read_file(filename$,array$())
OPEN #4:NAME filename$,create newold, org text, access outin
LET n=0
DO WHILE MORE #4
LET n=n+1
MAT REDIM array$(n)
LINE INPUT #4:array$(n)
LOOP
CLOSE #4
END SUB

These are general purpose routines. If you only have one piece of data then obviously the string ARRAY$ only has one element i.e. array$(1)=your_data$

Note that these sub-routines will handle numeric values, but you must first convert your numeric value to a string e.g.
LET array$(1)=str$(data)

Likewise you must convert strings back to numeric values when you read the data e.g.
LET data=val(array$(1))

Big John

Ok so far your response makes the most sense

Ok so far big Johns response makes the most sense, but i still dont fully understand. when i make my game its going to record high scores so it is going to have a list like this...

1.NAME --------> SCORE !name1$ --------> score1$
2.NAME --------> SCORE !name2$ --------> score2$
3.NAME --------> SCORE !name3$ --------> score3$
4.NAME --------> SCORE !name4$ --------> score4$
5.NAME --------> SCORE !name5$ --------> score5$

I need to know howto save All of the variables name1$ -name10$ and score1$ - score 10$ into a .dat file and then be able to read this information later in the program.

DAT files

Hi,

It doesn't matter what extension name you use, e.g. DAT or TXT or TRU or XYZ. What matters is the format of the file, i.e whether it is a text file or a record file or a byte file. The format will depend on how you wrote the file in the first place. To recover the data you need to read the file the same way as you wrote it.

If you have multiple scores and names then the easiest way to store the data is to create an array. To avoid mixing up names and scores I would recommend that you assign both names and scores to the same array element, e.g.
LET array$(1)=name1$ & "," & score1$
LET array$(2)=name2$ & "," & score2$

Now you can use the reading and writing routines I posted earlier.

When you read this file e.g.

LINE INPUT #1: array$(n)

The array$(n) variable will contain both the name AND the score, so you need to separate them as follows:
LET length=len(array$(n))
LET comma=cpos(array$(n),",")
LET name$=array$(n)[1:comma-1]
LET score$=array$(n)[comma+1:length]

Big John

I used the information

I used the information from the posts you have left, but I think I'm still having trouble. I tried to make a test program from the information you have provided me with and I don't think it works. here it the program I put togather with your subprogram...

!filename: Read_Write_to_file_test.tru

DIM array$(2)

Print "test"
input prompt "what is your name?" :name1$
input prompt "what is your score?" :score1$
print "press a key to continue"
get key pse

call write_file!(test$,array$(1))
clear
call read_file
!print name1$
!print score1$

LET array$(1)=name1$ & "," & score1$

SUB write_file!(test$,array$(1))
OPEN #4:NAME "test.txt",create newold, org text, access outin
ERASE #4
SET #4: MARGIN maxnum
LET total=ubound(array$)
FOR n=1 to total
PRINT #4:array$(n)
NEXT N
CLOSE #4
END SUB

end

I had made a .txt file with no characters in it and it showed as being 0KB. I ran the program and the file size went to 1KB. but i open it with notepad and there is nothing in it. I have also tried using a .dat file with the same effects.

Read/write text files

Hi,

Your problem is very simple. You asked the user to input the name and score using the variables name1$ and score1$. Very good so far. Now you call the appropriate sub-routines. Also very good. The bit you missed out was to tell the write routine what the name and score was. You passed the variable ARRAY$ to the sub-routine but you didn't assign the name and score to ARRAY$(1) and ARRAY$(2) e.g.
LET array$(1)=name1$
LET array$(2)=score1$

If you think about it - how is the write routine supposed to know what array$(1) and array$(2) are unless you tell it?

Regards
Big John

Re: Read &Write data to a data file ...

bubbachunk ... If you have access to the TB Silver user manual, the Subject: is as barely described as the TB manual writers could make it!!

Give me some time today so I can compose some code & post it on the Forum. Meanwhile, start reading about the "byte type" file in the manual. If you need to read data from a stored disk file and write data to a disk file you MUST use a byte-type file -- or you will likely be sorry you didn't!! Text-type files are lousy for handling "data".

Once you learn everything about byte files you'll never go back to text files. Regards ... Tom M