BBC BASIC for Windows
Programming >> Database and Files >> File Problems!
http://bb4w.conforums.com/index.cgi?board=database&action=display&num=1386769760

File Problems!
Post by samconran on Dec 11th, 2013, 10:59am

Hey guys,

Can anyone help me with this, please? So I want to set up a game where a number of characters (up to 4) play a game. At the start of the game some dice are thrown and one score is divided by another and added to ten. I want to store the data in a file but I don't know how to reopen a file and put in new information. So I want to store some data, close the file and then be able to open it and add more without deleting the original content. My code is below. Please help!

Thanks, Sam :)


Code:
      
      INPUT "Enter filename: " filename$
      
      INPUT "Enter number of players: " plays%
      
      FOR times%=1 TO plays%
        
        filename$ = filename$ + ".txt"
        
        filenum = OPENOUT(filename$)
        
        
        INPUT "Enter character's name: " c$

        
        strength1% = 10
        skill1% = 10
        
        strength2% = strength1%+(RND(12)/RND(4))
        skill2% = skill1%+(RND(12)/RND(4))

        
        PRINT ""
        PRINT ""
        PRINT c$ "'s strength is: "; strength2%
        PRINT ""
        PRINT c$ "'s skill is: "; skill2%
        
        

        
        PRINT#filenum, c$ + "'s strength is: " + STR$(strength2%)
        
        PRINT#filenum, c$ + "'s skill is: " + STR$(skill2%)
        
        
        
        
        CLOSE#filenum
        
        
        
      NEXT times%
      END
      
      
      CLOSE#filenum
 

Re: File Problems!
Post by admin on Dec 11th, 2013, 12:06pm

on Dec 11th, 2013, 10:59am, samconran wrote:
So I want to store some data, close the file and then be able to open it and add more without deleting the original content.

To append data to an existing file, open it (OPENUP) then move the pointer to the end (PTR#file% = EXT#file%). If you want to deal with the initial case when the file doesn't exist, try OPENUP first and if that fails use OPENOUT:

Code:
      file% = OPENUP(filename$)
      IF file%=0 THEN file% = OPENOUT(filename$)
      PTR#file% = EXT#file% 

Richard.

Re: File Problems!
Post by samconran on Dec 11th, 2013, 1:43pm

Okay thanks very much! I'll try and impliment that when I get the chance.