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 ........
Announcement
Collapse
No announcement yet.
Random Access Files
Collapse
X
-
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)
(Both the VSAM and BSAM file systems support this; these file systems run on IBM mainframes).
Leave a comment:
-
Originally posted by Stuart McLachlan View PostI 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
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...
Leave a comment:
-
Originally posted by Kurt Kuzba View PostWell, they're ALL binary files.
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
Leave a comment:
-
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)
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.
Leave a comment:
-
Originally posted by Michael Mattias View Post
There actually is no such thing as a "random access 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)
Leave a comment:
-
In random as files used to use field pb helpLOCAL 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
.
Leave a comment:
-
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 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.
Leave a comment:
-
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
Leave a comment:
-
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"
Leave a comment:
-
Originally posted by Gary Beene View PostFor 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.
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.
Leave a comment:
-
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!!!!
Leave a comment:
-
Originally posted by Tim Lakinir View PostThank you Rodney, but being a beginner I w'd like a more simple example code to start with
It goes through the steps necessary to put and get data to/from a random access file.
Leave a comment:
-
Thank you Rodney, but being a beginner I w'd like a more simple example code to start with
Leave a comment:
-
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
Leave a comment:
-
Random Access Files
Please add your comments and suggestions as a Reply to this thread.
PB/WIN - Random Access FilesRandom 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.See AlsoReferences
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
Code:WRITE #1, 0;0 ' takes 3 bytes PRINT #1, 0;0 ' takes 5 bytes PRINT #1, 1.660565E-27 ' takes 13 bytes
- First, OPEN the file and specify the length of each record:
Code:OPEN filespec FOR RANDOM AS [#]filenum [LEN = recordsize]
- 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
- 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
- Read records from the file using the GET statement.
Code:GET #fileNumber, recordNumber, Student
- When finished, CLOSE the file.
Last edited by Gary Beene; 28 Oct 2014, 10:13 PM.Tags: None
- First, OPEN the file and specify the length of each record:
Leave a comment: