Announcement

Collapse
No announcement yet.

Random Mode Files Record length

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

  • 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

  • #2
    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, 09:05 AM.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Put that code before the OPEN

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

      Then it will work
      Old QB45 Programmer

      Comment


      • #4
        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.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          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. "

          Comment


          • #6
            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.
            Old QB45 Programmer

            Comment


            • #7
              >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
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                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.
                Old QB45 Programmer

                Comment


                • #9
                  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.

                  Comment


                  • #10
                    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, 02:17 AM. Reason: Added link for multi-user and cgi

                    Comment

                    Working...
                    X