Jump to content

Photo

Simple file IO disk operations question


35 replies to this topic

#26 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • Topic Starter
  • 2,510 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Sat Jan 6, 2018 5:05 PM

I could probably pull all the Delete statements, I'll have to test.



#27 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • Topic Starter
  • 2,510 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Mon Feb 5, 2018 6:39 AM

So to follow up on this because it's something I haven't fully grasped.

This topic lead to the development of my console vs console number guessing game.

I am currently using (Yes I know how inefficient it is) one file per variable so when I need to know the value or change the value of a single variable, I know what file to open.

How do you handle this in a single file?

I have seen from the examples given that you can easily load and save ALL variables in a file but what if you only want to change one of those variables and then retrieve only one of those variables?

Do you really have to save them all (even if only one of their values changes) and then load them all later (if you need to retrieve only one value)?

 

Asked another way...

 

It's like a 1 dimensional array but you have no way to access and manipulate the single values in that array without reloading and then saving the entire array. Am I correct in that assumption?

 

If my assumption is wrong, then say I want to store a 2D array and then manipulate only a single value within it and reliably retrieve only a single value in it without loading and saving the entire array each time.

I'm going to guess that there is a lot of coding to take place in order to do such things and their is nothing "Built in" (DSR or otherwise) to help facilitate this.

 

I imagine you assembly coders approach this entirely differently and can probably store arrays to disk by dumping reallocated memory regions, let's try to keep this any answers grounded in BASIC, XB or RXB.

Because I'm no where near ready to start understanding assembly yet.


Edited by Sinphaltimus, Mon Feb 5, 2018 6:41 AM.


#28 Casey ONLINE  

Casey

    Moonsweeper

  • 278 posts

Posted Mon Feb 5, 2018 7:27 AM

Based on your question, I think you can do this with a relative file and get what you want.  Especially since you are essentially using a number of sequential files to do what the relative file can do for you.

 

Assume you have 5 variables you want to keep track of and save in the file, A-E - this uses Extended BASIC syntax just for a couple of lines to make my example easier, but all of this can be do in exactly the same way with TI BASIC.

10 A,B,C,D,E=1
20 OPEN #1:"DSK1.VARS",UPDATE,RELATIVE,FIXED
25 REM PUT THE 5 RECORDS ON THE FILE
30 PRINT #1:A::PRINT #1:B::PRINT #1:C::PRINT #1:C::PRINT #1:D::PRINT #1:E
35 REM CODE RUNS AND NOW UPDATE RECORD 3
40 PRINT #1,REC 2:25
45 REM WHAT WAS IN RECORD 1?
50 INPUT #1,REC 0:X
55 REM X+E INTO RECORD 5
60 PRINT #1,REC 4:X+E
65 REM NOW LET'S SEE WHAT'S IN ALL THE RECORDS
70 RESTORE #1
80 FOR I=0 TO 4::INPUT #1:Y::PRINT Y::NEXT I
90 CLOSE #1

With a relative file, you must used fixed length records, so that the computer knows where the next record starts.  But you can open a relative file in UPDATE mode, and then read and write any specific record in the file, or you can process them all just as if they were a sequential file.  The record numbers start at 0, so the 3rd record in the file is referenced by using REC 2 on your PRINT and INPUT statements.  

 

With just a little rework in your program, you can change from multiple files to multiple records in a single relative file fairly easily.


Edited by Casey, Mon Feb 5, 2018 7:28 AM.


#29 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • Topic Starter
  • 2,510 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Mon Feb 5, 2018 8:59 AM

Thanks Casey, I'll give this a go after work and see how well my reading comprehension is holding up.

#30 Opry99er OFFLINE  

Opry99er

    Quadrunner

  • 9,797 posts
  • Location:Hustisford, WI

Posted Mon Feb 5, 2018 2:24 PM

Id ask how many values we are talking about?

In my Werewolves Game, I pull 252 numeric variables from disk at the start of the game and populate a 2 dimensional array on the fly. I also pull 120 string variables from disk on each floor and populate a 2 dimensional string array on the fly. During any given point in the game, I can pull whatever value I need from either of these arrays and manipulate it. Then you can write a super small routine (3-4 lines of code) to break down the arrays in the same manner you built them and re-write your file to disk.

If youre maintaining 30-40 different variables with 30-40 different files, then I suggest using either Caseys method, or pulling the whole data set and then re-writing it once youve manipulated what you need. I can help you with this if youd like.

#31 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • Topic Starter
  • 2,510 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Mon Feb 5, 2018 3:31 PM

Id ask how many values we are talking about?

In my Werewolves Game, I pull 252 numeric variables from disk at the start of the game and populate a 2 dimensional array on the fly. I also pull 120 string variables from disk on each floor and populate a 2 dimensional string array on the fly. During any given point in the game, I can pull whatever value I need from either of these arrays and manipulate it. Then you can write a super small routine (3-4 lines of code) to break down the arrays in the same manner you built them and re-write your file to disk.

If youre maintaining 30-40 different variables with 30-40 different files, then I suggest using either Caseys method, or pulling the whole data set and then re-writing it once youve manipulated what you need. I can help you with this if youd like.


7. 7 total. My next game might be double that. :)



#32 Opry99er OFFLINE  

Opry99er

    Quadrunner

  • 9,797 posts
  • Location:Hustisford, WI

Posted Mon Feb 5, 2018 4:29 PM

You could do that in zero time, flat. :)

Would you like some code examples? Super simple stuff

#33 Opry99er OFFLINE  

Opry99er

    Quadrunner

  • 9,797 posts
  • Location:Hustisford, WI

Posted Mon Feb 5, 2018 4:34 PM

So let's start off with making your initial data file.  We will need to populate it with all the starting values of your variables.  The easiest way to do this is with a small, unrelated program (which is not part of your main game or variable handling routines).  This stands alone, and you only need to use it once.  After that, the main program will do all this for you.
 
 
 
Here we open a new file, we name it, we set it to be INTERNAL and then we proceed to populate it with numbers.
 
100 OPEN #1:"DSK1.TIPIDATA",INTERNAL
110 FOR X=1 TO 7
120 READ A
130 PRINT #1:A
140 NEXT A
150 CLOSE #1
1000 DATA 103,5,3,9,2,5,17
 
Thats it thats the whole program.
 
 
 
 
 
Now, for your game  this code will have to be in there. You can determine where you want to put it, based on your needs.
 
10 DIM A(7)  ***at the top of the program
 
 
100 OPEN #1:"DSK1.TIPIDATA",INTERNAL
110 FOR LOOP=1 TO 7
120 INPUT #1:A(LOOP)
130 NEXT LOOP
140 CLOSE #1
 
 
 
What this does is opens your data file and populates the array with the data in your disk.  You'll have 7 values in your array.  You can then manipulate them using whatever method you choose.
 
**For instance**
 
550 REM IF PLAYER INPUTS "1", THEN CLEAR OUT VALUE 'A(3)' AND MAKE IT 'ZERO'
560 CALL KEY(0,K,S)
580 IF K=49 THEN A(3)=0
 
 
 
 
 
Once you've manipulated the data however you want, you need only save the manipulated
data to diskette.  We do this by flipping the script from the initial method.
 
**NOTICE that the only difference here is that we PRINT instead of INPUT.
 
 
800 OPEN #1:"DSK1.TIPIDATA",INTERNAL
810 FOR LOOP=1 TO 7
820 PRINT #1:A(LOOP)
830 NEXT LOOP
840 CLOSE #1
 
And continue with your game.  The only thing you will have to remember is which entry in your array does what.  A simple comment in your code will help you keep track of that.

Edited by Opry99er, Mon Feb 5, 2018 4:46 PM.


#34 Sinphaltimus OFFLINE  

Sinphaltimus

    River Patroller

  • Topic Starter
  • 2,510 posts
  • Distracted at the Keyboard
  • Location:Poconos, PA

Posted Mon Feb 5, 2018 4:43 PM

Thanks for this clear example. This will work well for the existing game. I also have to write a TIPI version that handles variables very differently but remarkably as well. I've been chatting with the Jedi privately on those details.

On a side note, I'm guessing this
810 FOR LOOP=1 TO 36
is supposed to be this

810 FOR LOOP=1 TO 7

so the examples are in tune with each other. Correct?



#35 Opry99er OFFLINE  

Opry99er

    Quadrunner

  • 9,797 posts
  • Location:Hustisford, WI

Posted Mon Feb 5, 2018 4:47 PM

Correct. I fixed it, but on my phone it messed up the formatting. :)

#36 Opry99er OFFLINE  

Opry99er

    Quadrunner

  • 9,797 posts
  • Location:Hustisford, WI

Posted Mon Feb 5, 2018 4:49 PM

And again, Caseys method works just as well... this is just method #2 in the catskinners handbook.




0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users