BBC BASIC for Windows
Programming >> Database and Files >> Using PTR#A
http://bb4w.conforums.com/index.cgi?board=database&action=display&num=1227266628

Using PTR#A
Post by GordonSweet on Nov 21st, 2008, 10:23am

I can read all the bytes in say a TXT file like below or any other file. But how do I calculate the PTR#A to use BPUT#A to change a byte, or must I open the file for random access etc with fnum=OPENUP(filename$)

Thanks Gordon

Code:
      MODE 20 : COLOUR 0,255,255,255 : CLS
      COLOUR 7,0,0,0 : COLOUR 7
      DIM dat(1000)
      f$ = "1test.txt"
      A=OPENIN f$
      REPEAT
        COLOUR 7 :  c% = c% + 1 :
        k% = BGET#A : dat(c%) = k%
        IF k% > 31 AND k% <> 127 THEN PRINT CHR$(k%);
      UNTIL EOF#A
      CLOSE #A
      END 
 

Re: Using PTR#A
Post by admin on Nov 21st, 2008, 1:27pm

Quote:
But how do I calculate the PTR#fnum to use BPUT#A to change a byte, or must I open the file for random access etc with fnum=OPENUP(filename$)

You don't need to open a file with OPENUP to move its pointer; you can use PTR#fnum= to move the pointer whether the file was opened with OPENIN, OPENOUT or OPENUP. Obviously, when opened using OPENOUT you are restricted to moving the pointer to within the range you have already written.

As for calculating where to position the pointer, you should be able to work it out by knowing the number of bytes used for each item in the file. BPUT#fnum,n uses 1 byte, PRINT#fnum,n uses 5 bytes (in *FLOAT40 mode, 8 in *FLOAT64 mode) and PRINT#fnum,s$ uses LEN(s$)+1 bytes. Simply add together the sizes of the preceding items to find the current file pointer.

Richard.
Re: Using PTR#A
Post by GordonSweet on Nov 21st, 2008, 1:35pm

Thanks again Richard I will study it.

Gordon
Re: Using PTR#A
Post by GordonSweet on Nov 22nd, 2008, 10:18am

No luck Richard below shows Access Denied at line 210.
I expect I have got the length wrong Gordon

Code:
   10 MODE 20 : COLOUR 0,255,255,255 : CLS
   20 COLOUR 7,0,0,0 : COLOUR 7
   30 DIM dat(10000) : f$ = "test.txt"
   40 
   50 T=OPENOUT f$
   60 PRINT#T, "Now is the time for all good men to come to the aid of the party."
   70 CLOSE #T
   80 
   90 dat = 5 : byte = 34 : length = 5
  100 A=OPENIN f$
  110 base=PTR#A
  120 REPEAT
  130   COLOUR 7 :  c% = c% + 1
  140   k% = BGET#A : dat(c%) = k%
  150   IF k% > 31 AND k% <> 127 THEN PRINT "  ";CHR$(k%);
  160 UNTIL EOF#A OR c% > 30
  170 @% = 3 : PRINT
  180 FOR r = 1 TO 31 : PRINT r; : NEXT r : PRINT'
  190 PTR# A=FN_ptr(dat)
  200 BPUT#A,byte
  210 CLOSE#A : RUN : REM To test
  220 END
  230 
  240 DEF FN_ptr(record)=base+record*length
 

Re: Using PTR#A
Post by GordonSweet on Nov 23rd, 2008, 07:31am

Sorry to pester you Richard, I fathomed it out. The bytes here only increment by 1
Gordon

Code:
      MODE 20 : COLOUR 0,255,255,255 : CLS
      COLOUR 7,0,0,0 : COLOUR 7
      f$ = "test.txt"
      
      T=OPENOUT f$
      PRINT#T, "Now is the time for all good men to come to the aid of the party."
      CLOSE#T
      PROCshow
      
      REM insert
      B=OPENUP f$
      PTR#B=4 : BPUT#B,35
      CLOSE#B
      PROCshow
      END
      
      DEF PROCshow
      A=OPENIN f$
      INPUT#A,dat$ : PRINT dat$
      CLOSE #A : ENDPROC