home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 08 / field.bas < prev    next >
BASIC Source File  |  1991-03-29  |  2KB  |  59 lines

  1. 'FIELD.BAS - demonstrates opening, reading, and writing using FIELD
  2.  
  3. DECLARE SUB OpenFile (FileName$, RecLen%, Dat$(), FldLen%(), FileNum%)
  4. DECLARE SUB ReadFile (FileNum%, RecordNumber%)
  5. DECLARE SUB WriteFile (FileNum%, RecordNumber%)
  6.  
  7. CONST Max% = 5                  'we'll use five data fields
  8. FileName$ = "FIELD.DAT"         'this is the file name to open
  9. REDIM Contents$(1 TO Max%)      'this holds the file data contents
  10. REDIM Lengths%(1 TO Max%)       'this holds the field lengths
  11.  
  12. RecLength% = 0                  'this accumulates the total record length
  13. FOR X% = 1 TO Max%              'read the array of field lengths
  14.   READ Lengths%(X%)
  15.   RecLength% = RecLength% + Lengths%(X%)
  16. NEXT
  17.  
  18. '----- open the data file and assign some sample data
  19. CALL OpenFile(FileName$, RecLength%, Contents$(), Lengths%(), FileNumber%)
  20. FOR X% = 1 TO Max%
  21.   READ Temp$
  22.   LSET Contents$(X%) = Temp$
  23. NEXT
  24.  
  25. RecNumber% = 1
  26. CALL WriteFile(FileNumber%, RecNumber%) 'write the data to record number 1
  27. CALL ReadFile(FileNumber%, RecNumber%)  'read the data back again
  28.  
  29. FOR X% = 1 TO Max%                      'display the data to prove it worked
  30.   PRINT "{"; Contents$(X%); "}"
  31. NEXT
  32.  
  33. CLOSE #FileNumber%                      'close the file
  34.  
  35.  
  36. DATA 20, 25, 17, 20, 2
  37. DATA Tony Smith, Abco Corporation, 2200 Baxter Place, Atlanta, GA
  38.  
  39. SUB OpenFile (FileName$, RecLen%, Dat$(), FldLen%(), FileNum%) STATIC
  40.   FileNum% = FREEFILE                   'get the next available file number
  41.   OPEN FileName$ FOR RANDOM AS #FileNum% LEN = RecLen%    'open the data file
  42.   NumFields% = UBOUND(FldLen%)          'see how many fields there are
  43.   Pad = 0                               'accumulates the padding needed
  44.   FOR X% = 1 TO NumFields%
  45.     ThisLen% = FldLen%(X%)              'see how long this field is
  46.     FIELD #FileNum%, Pad AS Dummy$, ThisLen% AS Dat$(X%)  'field it
  47.     Pad = Pad + ThisLen%                                  'advance the pad
  48.   NEXT
  49. END SUB
  50.  
  51. SUB ReadFile (FileNum%, RecordNumber%) STATIC
  52.   GET #FileNum%, RecordNumber%
  53. END SUB
  54.  
  55. SUB WriteFile (FileNum%, RecordNumber%) STATIC
  56.   PUT #FileNum%, RecordNumber%
  57. END SUB
  58.  
  59.