Announcement

Collapse
No announcement yet.

Random Mode Files Record length

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mike Doty
    replied
    Code:
    #COMPILE EXE
    #DIM ALL
    FUNCTION PBMAIN () AS LONG
     
      LOCAL a AS STRING
      LOCAL st AS INTEGER
      LOCAL b AS STRING
      LOCAL stb AS INTEGER
      LOCAL c AS STRING
     
      OPEN "new.dat" FOR RANDOM AS #1 LEN =20
      A$ = "12345678911234567892"
      St% = LEN(A$)
      PRINT St%, A$
      PUT #1,1, ABS A$  'added ABS
      CLOSE #1
     
      OPEN "new.dat" FOR RANDOM AS #2 LEN=20
      b = SPACE$(20)    'required
      GET #2,1, ABS B$  'added ABS
      Stb% = LEN( B$)
      PRINT Stb%,B$
      INPUT C$
     
    END FUNCTION
    Code:
    IF you want multi-user or cgi THEN
      [URL]http://www.powerbasic.com/support/pbforums/showthread.php?t=24345[/URL]
    END IF
    Last edited by Mike Doty; 8 Aug 2009, 01:17 AM. Reason: Added link for multi-user and cgi

    Leave a comment:


  • Nick Luick
    replied
    Good method Guy that I implemented years ago in DOS days when space was more critical. I found that its rare to have all allocated string lengths maxed out.

    Leave a comment:


  • Guy Dombrowski
    replied
    I have fixed length random customer files but the section for the name and address is made of dynamic string separated with a chr$(13).
    So although the maximum per line is 40 characters, the average is a lot less than that. Use PARSE to read back the data.

    Leave a comment:


  • Michael Mattias
    replied
    >it would be better to switch to sequential mode.
    That may be true in this application, but we don't don't know enough to say that with confidence.

    >To use random mode you have to know in advance the size of each field

    No, you don't. The length word handles that for you. You just have to make sure your record length is at least SIZEOF(LONG) + max dynamic string length. (It also wastes disk, but that may be a price worth paying for the benefits of random access).

    MCM

    Leave a comment:


  • Guy Dombrowski
    replied
    If all you want is write is variable length string, it would be better to switch to sequential mode.
    To use random mode you have to know in advance the size of each field.
    Still better would be the Binary mode.

    Leave a comment:


  • John Petty
    replied
    From the manual "When writing a dynamic string to a random access file, PUT writes a 2-byte descriptor containing the string's length, before the actual string data. This descriptor reduces the available space in a record by two bytes. "

    Leave a comment:


  • Michael Mattias
    replied
    Code:
     DIM a AS STRING * 20
     DIM b as STRING * 20
    Then it will work
    If that's all you change, no it won't. Your records will be space-filled to 20 bytes, which is not what the original code is trying to do, which is to store dynamic (dynamic = variable-length) string data.

    You, too, might want to mosey on back over to the help file pages for the PUT and GET statements and check out some of the options.

    Leave a comment:


  • Guy Dombrowski
    replied
    Put that code before the OPEN

    DIM a AS STRING * 20
    DIM b as STRING * 20

    Then it will work

    Leave a comment:


  • Michael Mattias
    replied
    Because when you write a dynamic string variable to a file opened FOR RANDOM, you're not really writing just the string unless you do something special.

    Look up the options for the PUT and GET statements again.
    Last edited by Michael Mattias; 7 Aug 2009, 08:05 AM.

    Leave a comment:


  • Charles Perry
    started a topic Random Mode Files Record length

    Random Mode Files Record length

    Below is a single record write to a random access file with the consol screen output below.
    Why is the read back only 18 bytes when the write string is 20 bytes
    from the LEN = 20. Are these 2 missing bytes the string length?

    #COMPILE EXE
    '#DIM ALL
    FUNCTION PBMAIN () AS LONG
    OPEN "f:\pb35\new.dat" FOR RANDOM AS #1 LEN =20
    A$ = "12345678911234567892"
    St% = LEN(A$)
    PRINT St%, A$
    PUT #1,1,A$
    CLOSE #1
    OPEN "F:\pb35\new.dat" FOR RANDOM AS #2 LEN=20
    GET #2,1,B$
    Stb% = LEN( B$)
    PRINT Stb%,B$
    INPUT C$
    END FUNCTION
    20 12345678911234567892
    18 123456789112345678
Working...
X