Announcement

Collapse
No announcement yet.

Random Access Files

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

  • Random Access Files

    Please add your comments and suggestions as a Reply to this thread.





    PB/WIN - Random Access Files
    Random access files consist of records that can be accessed in any sequence. This means the data is stored exactly as it appears in memory, thus saving processing time (because no translation is necessary) both in when the file is written and in when it is read.

    Random files are a better solution to database problems than sequential files, although there are a few disadvantages. For one thing, random files are not especially transportable. Unlike sequential files, you cannot peek inside them with an editor, or type them in a meaningful way to the screen. In fact, moving a PowerBASIC random file to another computer or language will probably require that you write a translator program to read the random file and output a text (sequential) file.

    One example of the transportability problem strikes close to home. Interpretive BASIC uses Microsoft's non-standard format for floating-point values, and PowerBASIC uses IEEE standard floating-point conventions, this means you cannot read the floating-point fields of random files created by Interpretive BASIC with a PowerBASIC program, or vice versa, without a bit of extra work.

    The major benefit of random files is implied in their name: every record in the file is available at any time. For example, in a database of 23,000 alumni, a program can go straight to record number 11,663 or 22,709 without reading any of the other records. This capability makes it the only reasonable choice for large files, and probably the better choice for small ones, especially those with relatively consistent record lengths.

    However, random access files can be wasteful of disk space because space is allocated for the longest possible field in every record. For example, a 100-byte comment field forces every record to use an extra 100 bytes of disk space, even if only one in a thousand actually uses it.

    At the other extreme, if records are consistent in length, especially if they contain mostly numbers, random files can save space over the equivalent sequential form. In a random file, every number of the same type (Integer, Long-integer, Quad-integer, Byte, Word, Double-word, Single-precision, Double-precision, Extended-precision or Currency) occupies the same amount of disk space, regardless of the value itself. For example, the following five Single-precision values each require four bytes (the same space they occupy in memory):

    Code:
    0
    1.660565E-27
    15000.1
    641
    623000000
    By contrast, numbers in a sequential file require as many bytes as they have ASCII characters when printed (plus one for the delimiting comma if WRITE# was used instead of PRINT#). For example:

    Code:
    WRITE #1, 0;0           ' takes 3 bytes
    PRINT #1, 0;0           ' takes 5 bytes
    PRINT #1, 1.660565E-27  ' takes 13 bytes
    You can create, write, and read random access files using the following steps:
    1. First, OPEN the file and specify the length of each record:

      Code:
      OPEN filespec FOR RANDOM AS [#]filenum [LEN = recordsize]
      The LEN parameter indicates to PowerBASIC the total size of each record in bytes. If you do not specify a LEN parameter, PowerBASIC assumes 128. Unlike sequential files, you do not have to declare whether you are opening for input or output because you can simultaneously read and write a random file.
    2. Define a structure for records in the file using the TYPE statement.

      Code:
      TYPE StudentRecord
        LastName     AS STRING * 20 ' A 20-character string
        FirstName    AS STRING * 15 ' A 15-character string
        IDnum        AS LONG        ' Student ID, a Long-integer
        Contact      AS STRING * 30 ' Emergency contact person
        ContactPhone AS STRING * 14 ' Their phone number
        ContactRel   AS STRING * 8  ' Relationship to student.
      
        AverageGrade AS SINGLE      ' Single-precision % grade
      END TYPE
      DIM Student AS StudentRecord
    3. Fill the UDTs members with the values you want, and write records to the file using the PUT statement.

      Code:
      Student.LastName = "Anderson"
      Student.FirstName = "Bob"
      Student.IDnum = 494425610
      Student.Contact = "Ma Anderson"
      Student.ContactPhone = "(800) BOBSMOM"
      Student.ContactRel = "Mother"
      Student.AverageGrade = 98.9
      PUT #fileNumber, recordNumber, Student
    4. Read records from the file using the GET statement.

      Code:
      GET #fileNumber, recordNumber, Student
    5. When finished, CLOSE the file.
    See AlsoReferences
    Last edited by Gary Beene; 28 Oct 2014, 10:13 PM.

  • #2
    Hi Gary, can you please provide an example code of random file, something that kinda write data into it and then able to retrieve a record base on a GET statement.
    Thank you in advance

    Comment


    • #3
      C:\PBWin10\Samples\DDT\Address\Address.bas
      Rod
      I want not 'not', not Knot, not Knott, not Nott, not knot, not naught, not nought, but aught.

      Comment


      • #4
        Thank you Rodney, but being a beginner I w'd like a more simple example code to start with

        Comment


        • #5
          Originally posted by Tim Lakinir View Post
          Thank you Rodney, but being a beginner I w'd like a more simple example code to start with
          Search for "Random Access Files" in Hep and follow the instructions at the bottom of the page.

          It goes through the steps necessary to put and get data to/from a random access file.

          Comment


          • #6
            Sorry, I missed target. Never mind.

            Comment


            • #7
              Originally posted by Jim Fritts View Post
              'Version V8 testing on my system (Core2 Duo, mechanical drive, 4 GiB RAM, No hyperthreading)
              'These are the most recent values captured from Process Time 2020-SEP-05
              'They document the lowest values Process Time can have when testing the app multiple times for each thread selection. These are not average values.

              'Threads used and the Process Time in seconds:
              '1) 10.193 <<< not retested
              '2) 5.725
              '4) 5.542
              '5) 5.437
              '10) 5.401
              '20) 5.363
              '25) 5.357
              ????????
              I think you missed!!!!
              Dale

              Comment


              • #8
                Originally posted by Gary Beene View Post
                For one thing, random files are not especially transportable. Unlike sequential files, you cannot peek inside them with an editor, or type them in a meaningful way to the screen. In fact, moving a PowerBASIC random file to another computer or language will probably require that you write a translator program to read the random file and output a text (sequential) file.
                If portability is an issue, random files should be limited to plain text in fixed length string fields.
                This will increase the size of the file, by inflating a two or four byte value to a string of characters to represent numbers.
                This is a design phase consideration, although conversion to delimited text or fixed length text may always be included in the source code using the random files.

                The world is strange and wonderful.*
                I reserve the right to be horrifically wrong.
                Please maintain a safe following distance.
                *wonderful sold separately.

                Comment


                • #9
                  Just realised what sub-forum this is in. It would be better to take this sort of discussion to one of the programming areas rather that clogging up "PB/Win Online Help & Commentary"

                  Comment


                  • #10
                    Code:
                    TYPE StudentRecord   'see post #1
                      LastName     AS STRING * 20 ' A 20-character string
                      FirstName    AS STRING * 15 ' A 15-character string
                      IDnum        AS LONG        ' Student ID, a Long-integer
                      Contact      AS STRING * 30 ' Emergency contact person
                      ContactPhone AS STRING * 14 ' Their phone number
                      ContactRel   AS STRING * 8  ' Relationship to student.
                      AverageGrade AS SINGLE      ' Single-precision % grade
                    END TYPE
                    
                    FUNCTION PBMAIN () AS LONG
                     LOCAL filespec, s AS STRING
                     LOCAL filenumber, recordsize,recordnumber AS LONG
                     LOCAL Student AS StudentRecord
                    
                     filespec   = "StudentFile.dat"
                     recordsize =  LEN(Student)
                     filenumber = FREEFILE
                     OPEN filespec FOR RANDOM AS #filenumber LEN = recordsize
                    
                     recordnumber = LOF(filenumber)\recordsize + 1
                     Student.LastName      = "Anderson"
                     Student.FirstName     = "Bob"
                     Student.IDnum         = 494425610
                     Student.Contact       = "Ma Anderson"
                     Student.ContactPhone  = "(800) BOBSMOM"
                     Student.ContactRel    = "Mother"
                     Student.AverageGrade  = 98.9
                    
                     PUT #filenumber, recordNumber, Student
                     GET #filenumber, recordnumber, Student
                     s = Student.LastName                   'AS STRING * 20 A 20-character string
                     s+= Student.FirstName                  'AS STRING * 15 A 15-character string
                     s+= USING$("#",Student.IDnum)          'AS LONG       ' Student ID, a Long-integer
                     s+= Student.Contact                    'AS STRING * 30 ' Emergency contact person
                     s+= Student.ContactPhone               'AS STRING * 14 ' Their phone number
                     s+= Student.ContactRel                 'AS STRING * 8  ' Relationship to student.
                     s+= USING$("#.#",Student.AverageGrade) 'AS SINGLE      ' Single-precision % grade
                     ? s,, USING$("recordnumber #",recordnumber)
                     CLOSE #filenumber
                    END FUNCTION
                    How long is an idea? Write it down.

                    Comment


                    • #11
                      Random access files consist of records that can be accessed in any sequence. This means the data is stored exactly as it appears in memory, thus saving processing time (because no translation is necessary) both in when the file is written and in when it is read.
                      There actually is no such thing as a "random access file." All Files are the same; however, some have a logic to the storage of data which allows the programmer to read the portion of the file of interest - a "record" - without the need to to read the entire file from the beginning and locate the "data of interest" that way. That is, the file's data structure permits the programmer to use a specific kind of access.

                      RANDOM access is the BASIC-language terminology for opening a file containing records of all the same fixed length and supporting the reading or writing of a record by its relative positional record number in the file, e.g, record one, record thirty-eight or record 123,456.

                      In PowerBASIC, "RANDOM" access is one of the TWO ways to open a file for random access; the other is BINARY.

                      That said, calling a file a "random file" colloquially means the file data consists of only fixed-length records of the same length.
                      Michael Mattias
                      Tal Systems Inc. (retired)
                      Racine WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        In random as files used to use field pb help
                        LOCAL sFirst AS FIELD, sSecond AS FIELD

                        OPEN "ABC.TXT" FOR RANDOM AS #1 LEN=20

                        FIELD #1, 10 AS sFirst, 10 AS sSecond

                        sFirst = "0123456789"

                        sSecond = "9876543210"

                        Put #1 ' creates a record of: 0123456789987654321

                        ====
                        LOCAL x$, sFirst AS FIELD, sSecond AS FIELD

                        FIELD x$, 3 AS sFirst, 3 AS sSecond

                        x$ = SPACE$(6) ' Allocate the space for the field

                        SFirst = "111"

                        sSecond = "222"

                        ? x$ ' Displays 111222

                        x$ = "abcd"

                        ? sFirst ' Displays abc

                        ? sSecond ' Displays d


                        one set fields
                        or set up types
                        ' Random-access GET example

                        DIM uName AS STRING * 20

                        DIM I AS QUAD

                        DIM F AS LONG



                        F = FREEFILE

                        OPEN "TESTFILE.DTA" FOR RANDOM AS #F LEN = LEN(uName)

                        WHILE uName <> SPACE$(20)

                        PUT #F,, uName

                        uName = GetInput()

                        WEND



                        IF SEEK(F) > 0 THEN

                        ShowText "The file contains these names:"

                        FOR ix = 1 TO SEEK(F)

                        GET #F, ix, uName

                        ShowText uName + NL

                        NEXT

                        the later is my preferred.

                        the File can be read in (by) other programs
                        i have an inventory program that edit the inventory of a line of inventory
                        the other is an invoicing program.
                        another runs a quarterly report , annual report
                        the program is being rewritten from pbcc to pbwin

                        just mine input

                        thanks

                        .

                        Comment


                        • #13
                          Originally posted by Michael Mattias View Post

                          There actually is no such thing as a "random access file."
                          By the same token, there is no such thing as a "comma separated file", a "tab delimited file", "executable file" or any other specific type of file.

                          But it is convenient to describe a file by the way its contents are intended to be used and "random access" is a commonly understood descriptor.

                          There are in fact two generally recognised structures for random access files:

                          1. Fixed length records
                          2. An index block in the file to permit "jumping" to a specific record. (Zip files are a cassic example fo this)



                          Comment


                          • #14
                            Originally posted by Stuart McLachlan View Post

                            By the same token, there is no such thing as a "comma separated file", a "tab delimited file", "executable file" or any other specific type of file.

                            But it is convenient to describe a file by the way its contents are intended to be used and "random access" is a commonly understood descriptor.

                            There are in fact two generally recognised structures for random access files:

                            1. Fixed length records
                            2. An index block in the file to permit "jumping" to a specific record. (Zip files are a cassic example fo this)


                            Well, they're ALL binary files.
                            And, since you know the record size, or have an index as a reasonable alternative, then it is possible to open in binary and load a specific number of bytes at a specific position.
                            That IS what random file access does, but only with fixed length files. Using a file index and binary access allows greater flexibility.
                            The world is strange and wonderful.*
                            I reserve the right to be horrifically wrong.
                            Please maintain a safe following distance.
                            *wonderful sold separately.

                            Comment


                            • #15
                              Originally posted by Kurt Kuzba View Post
                              Well, they're ALL binary files.
                              I guess that depends on definitions again.

                              A very common definition identifies files as either Binary or Text where a Text file is defined as a file where every byte represents an ANSI character (or maybe just printable ANSI character) and a binary file is anything else.

                              But then again, many will include files composed of UTF8 or UTF16 encoded data under Text file.

                              For a more detailed discussion see
                              https://dev.to/sharkdp/what-is-a-binary-file-2cf5

                              Comment


                              • #16
                                Originally posted by Stuart McLachlan View Post
                                I guess that depends on definitions again.

                                A very common definition identifies files as either Binary or Text where a Text file is defined as a file where every byte represents an ANSI character (or maybe just printable ANSI character) and a binary file is anything else.

                                But then again, many will include files composed of UTF8 or UTF16 encoded data under Text file.

                                For a more detailed discussion see
                                https://dev.to/sharkdp/what-is-a-binary-file-2cf5
                                Making text files a subset of binary data files?
                                Like the RANDOM designation, opening as a text file presumes a few things.
                                You can read sequential text in a BINARY file just fine.
                                Why, back in the OLD DAYS... mumble, mumbl, mmbl...
                                The world is strange and wonderful.*
                                I reserve the right to be horrifically wrong.
                                Please maintain a safe following distance.
                                *wonderful sold separately.

                                Comment


                                • #17
                                  There are in fact two generally recognised structures for random access files:

                                  1. Fixed length records
                                  2. An index block in the file to permit "jumping" to a specific record. (Zip files are a cassic example fo this)
                                  Note that some file systems support random access by record number even with variable-length records. None of these systems AFAIK run under Windows.

                                  (Both the VSAM and BSAM file systems support this; these file systems run on IBM mainframes).
                                  Michael Mattias
                                  Tal Systems Inc. (retired)
                                  Racine WI USA
                                  [email protected]
                                  http://www.talsystems.com

                                  Comment


                                  • #18
                                    It depends on the trade offs you are happy to wear, old fixed length records can waste a lot of storage space if the fixed length of each record is not filled. Attach an index to variable length records and insertions become a lot more complicated and you need to do a pointer sort from time to time. An operating system is well geared to manage large counts of individual variable length files located by an index but are no real joy to search for content.

                                    At its simplest in "programmer speak", an array of pointers to data locations. A tree pointer structure is faster than a linear index and both can manage variable length data but its a case of pick the trade off, speed, flexibility, maintenance (sorting and coherence), data storage efficiency ........
                                    hutch at movsd dot com
                                    The MASM Forum

                                    www.masm32.com

                                    Comment

                                    Working...
                                    X