Random Files
Random Files
A random file is made up of a series of records of identical length. A record can correspond to a scalar
data type, such as Integer or String, or to a user-defined type, in which each record is broken down into
fields corresponding to the members of the type.
where recordLength is the length of each record in the file. The default length is 128 bytes.
Because records in a random file must have the same length, elements of a type should be fixed-length.
If a string copied into a file record contains fewer characters than the record's fixed length, the
remainder of the record is left unchanged. However, if a string is too long for a record, it is truncated
when written.
String fields inside the user-defined type should also be fixed-length. If you do use variable-length, make
sure that the Len part of the Open statement specifies a length large enough to hold the longest strings.
The Len function can't give you a reliable value for the length of the record; you will need to estimate
that. You also can't navigate between records by omitting the record number in the Get and Put
statements.
For example:
Type emploRec
End Type
The length of a type can be determined at run time using the Len function.
For example, this record is 78 bytes long, so supply Len = 78 in the Open statement.
Use the Put statement to write to a random file. Put takes three parameters: the file number, the record
number, and a variable containing the data you wish to write. You can use Put to add or replace records,
but not to delete them. To replace a record in a random file, use its record number.
For example:
recNum = 5
To add new records to a random file, use a record number equal to one more than the number of
records in the file. To add a record to a file that contains 5 records, for example, use a position of 6.
To replace a record from a random file, create a new file and copy all the valid records from the original
file into the new file. Close the original file and use the Kill statement to delete it. Use the Name
statement to rename the new file to the same name as the original. You can also move each record,
following it "up" by one position, thus writing over the record. The problem with this technique is that it
leaves a duplicate record at the end of the file.
For example:
Next I
Use the Get statement to read from a random file into variables.
This example reads from the file numbered idFile, at record number 5, into the variable recHold.
Related information
File Handling
Binary files
Sequential files
By clicking this box, you acknowledge that you are NOT a U.S. Federal Government employee or agency,
nor are you submitting information with respect to or on behalf of one. HCL provides software and
services to U.S. Federal Government customers through its partners Four, Inc. Contact this team
at https://hcltechsw.com/resources/us-government-contact. Do not include any personal data in this
Comment box. Note: To report a problem or question about the product software, do not use this form.
Instead, go to the HCL Software Support site. Personal data should not be shared in this comment box.
See our Privacy Statement to understand how personal data is used.