0% found this document useful (0 votes)
13 views3 pages

Random Files

Uploaded by

anhnnhe186631
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Random Files

Uploaded by

anhnnhe186631
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

Opening random files

The syntax is:

Open fileName For Random As fileNumber [Len = recordLength]

where recordLength is the length of each record in the file. The default length is 128 bytes.

If the file does not exist, it is created.

Defining record types

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.

User-defined types can be used to define compound records.

For example:

Type emploRec

id As Integer ' Integers are 2 bytes long

salary As Currency ' Currency is 8 bytes

hireDate As Double ' Dates are also 8 bytes

lastName As String * 15 ' Fixed-length string of 30 bytes

firstName As String * 15 ' Fixed-length string of 30 bytes

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.

Dim recLen As Integer, idFile As Integer

Dim recHold As emploRec


idFile = 1 ' The file number to use for

' this file

recLen = Len(recHold) ' The record length for this file

Open "DATA.DAT" For Random As idFile Len = recLen

Writing to random files in LotusScript®

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:

Dim recNum As Integer

recNum = 5

' Replace record 5 with the contents of recHold.

Put idFile, recNum, recHold

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:

Dim tempRec As emploRec

For I = recNum To lastRec - 1

Get idFile, I + 1, tempRec

Put idFile, I, tempRec

Next I

Reading from random files

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.

' The record number to retrieve from the file

Dim recNum As Integer


recNum = 5

' The variable to read into

Dim recHold As emploRec

Get idFile, recNum, recHold

Related information

 File Handling

 Binary files

 Sequential files

To add feedback about this topic, select the following checkbox:

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.

You might also like