[futurebasic] Re: Reading Records Question

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : March 1998 : Group Archive : Group : All Groups

From: Deepesh Letap <DLetap@...>
Date: Mon, 30 Mar 1998 08:03:57 +0100
>I would like to be able to read in or write out just a single field of a
>record at times. How do I position the file pointer to just read in one field?

There are two obvious ways to access a specific field given the code so
far. There are advantages and disadvantages to both.

For method one, use the RECORD statement with the optional PositionInRecord
Parameter as follows:

LOCAL FN ReadMyField(fileID, recNum,FieldNumber%)
  SELECT CASE FieldNumber%
    CASE 1
      PositionInRecord=0
      RECORD fileID, recNum, PositionInRecord
      READ #fileID, gMyRecord.name$;31
    CASE 2
      PositionInRecord=32
      RECORD fileID, recNum, PositionInRecord
      READ #fileID, gMyRecord.address$;31
    CASE 3
      PositionInRecord=64
      RECORD fileID, recNum, PositionInRecord
      READ #fileID, gMyRecord.city$;31
    CASE 4
      PositionInRecord=96
      RECORD fileID, recNum, PositionInRecord
      READ #fileID, gMyRecord.StateZip$;31
  END SELECT
END FN

Method two is probably better in my opinion but it is really personal choice:

LOCAL FN ReadMyField(fileID, recNum,FieldNumber%)
  DIM TempRecordSpace.gMyRecord
  RECORD fileID, recNum
  READ #fileID, TempRecordSpace
  SELECT CASE FieldNumber%
    CASE 1
      gMyRecord.name$=TempRecordSpace. name$
    CASE 2
      gMyRecord.address$=TempRecordSpace.address$
    CASE 3
      gMyRecord.city$=TempRecordSpace.city$
    CASE 4
      gMyRecord.StateZip$=TempRecordSpace.StateZip$
  END SELECT
END FN

>How do I know where to set the file pointer?

You would use the record offsets that you defined in your DIM RECORD
section. Each string is 31 characters, plus add a byte for the length byte
hence the total is 32 bytes. If the figure was odd, you would add one more
to make it even (eg a 32 character string would be 32 bytes of data, 1 byte
of length byte, totalling 33 bytes. Not even so add one more to make 34
bytes)

>Also, is just reading one field in this way faster than reading the whole
>record, or does it really make much difference? My application reads a lot of
>records so I want it to be as fast as possible.

In terms of speed, the time taken to read a record or an individual field
is probably very little different. The time is spent more on overhead when
bytes vary from 32 or 128 as in your case. If you need to read more than
one field but not the whole record, using the TempRecord method (method
two) is better because only one disk access is made.

Hope this helps,

Deep





>I have an application that reads and writes records much like this.
>
>'---------------------- Globals ---------------------------
>DIM RECORD gMyRecord                       'sample mail list
>  DIM 31 name$                                'record
>  DIM 31 address$
>  DIM 31 city$
>  DIM 31 StateZIP$
>DIM END RECORD_gMyRecord
>END GLOBALS
>
>'---------------------- Functions -------------------------
>LOCAL FN readRecord (fileID, recNum)
>  RECORD fileID, recNum                   'pos file pointer
>  READ #fileID, gMyRecord                 'read record in
>END FN
>
>LOCAL FN writeRecord (fileID, recNum)
>  RECORD fileID, recNum                   'pos file pointer
>  WRITE #fileID, gMyRecord                'write record out
>END FN
>
>I would like to be able to read in or write out just a single field of a
>record at times. How do I position the file pointer to just read in one field?
>How do I know where to set the file pointer?
>
>LOCAL FN readRecord (fileID, recNum)
>  RECORD fileID, recNum, address%  <=== How do I know what number to use here?
>  READ #fileID, gMyRecord.address$  <=== Will this get me just the one field
>text I want?
>END FN