Q(uick)BASIC Function: SEEK
Quick View
SEEK
A file I/O function that returns the current file position
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- SEEK(filenumber%)
- SEEK [#]filenumber%, position&
Description/Parameter(s)
- The SEEK function returns the current file position.
- The SEEK statement sets the file position for the next read or write.
filenumber% | The number of an open file. |
position& | The position where the next read or write occurs. For random-access files, a record number. For other files, the byte position relative to the beginning of the file. The first byte is at position 1. |
Example
OPEN "TEST.DAT" FOR RANDOM AS #1
FOR i% = 1 TO 10
PUT #1, , i%
NEXT i%
SEEK #1, 2
GET #1, , i%
PRINT "Data: "; i%; " Current record: "; LOC(1); " Next: "; SEEK(1)
See also:
Syntax
- SEEK(filenumber)
Description/Parameter(s)
The filenumber is the number used in the OPEN statement to open the file. SEEK returns a value in the range 1 to 2,147,483,647 (equivalent to 2^31 -1).
SEEK returns the number of the next record read or written when used on RANDOM mode files. For files opened in BINARY, OUTPUT, APPEND, or INPUT mode, SEEK returns the byte position in the file where the next operation is to take place. The first byte in a file is 1.
When used on a device that does not support SEEK, the function returns zero. The BASIC devices (SCRN:, CONS:, KYBD:, COMn:, and LPTn:) do not support SEEK.
The following code fragment prints a message indicating whether the last read or write was done in the first, second, or final third of the file:
- SELECT CASE (SEEK(1))
- CASE IS < .333*LOF(1)
- PRINT "In first third of file."
- CASE .333*LOF(1) TO .667*LOF(1)
- PRINT "In second third of file."
- CASE IS >= .667*LOF(1)
- PRINT "In last third of file."
- CASE ELSE
- END SELECT
Example
See the ⮜ SEEK statement programming example ⮞, which uses both the SEEK function and the SEEK statement.
Syntax
- SEEK(filenumber&)
Description/Parameter(s)
filenumber& | The number used in the OPEN statement to open the file. |
returns: | |
For RANDOM mode files: | The number of the next record read or written. |
For other files: | The byte position where the next operation occurs. |
For nonsupporting devices: | 0. |
Usage Notes
- SEEK returns a value between 1 and 2,147,483,647, inclusive (equivalent to 2^31 -1).
- When used on random-access files, SEEK returns the number of the next record read or written.
- For files opened in binary, output, append, or input mode, SEEK returns the byte position in the file where the next operation is to take place. The first byte in a file is 1.
- SEEK returns zero when used on an ISAM table or on BASIC devices (SCRN, CONS, KYBD, COMn, LPTn, PIPE) that do not support SEEK.
Programming Tips
- The following code fragment prints a message indicating whether the last read or write was done in the first, second, or final third of the file:
- SELECT CASE (SEEK(1))
- CASE IS < .333*LOF(1)
- PRINT "In first third of file."
- CASE .333*LOF(1) TO .667*LOF(1)
- PRINT "In second third of file."
- CASE IS >= .667*LOF(1)
- PRINT "In last third of file."
- CASE ELSE
- END SELECT
Example
The following program uses the SEEK function and the SEEK statement to move the file position exactly one record back and rewrite the record if a variable is true (nonzero). The RESTORE statement is used to set the data pointer to the first DATA statement.
OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN = LEN(RecordVar)
CLS
RESTORE
READ NameField$, ScoreField
I = 0
DO WHILE NameField$ <> "END"
I = I + 1
RecordVar.NameField = NameField$
RecordVar.ScoreField = ScoreField
PUT #1, I, RecordVar
READ NameField$, ScoreField
LOOP
CLOSE #1
DATA "John Simmons", 100
DATA "Allie Simpson", 95
DATA "Tom Tucker", 72
DATA "Walt Wagner", 90
DATA "Mel Zucker", 92
DATA "END", 0
'Open the test data file.
DIM FileBuffer AS TestRecord
OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN = LEN(FileBuffer)
'Calculate number of records in the file.
Max = LOF(1) \ LEN(FileBuffer)
'Read contents of each record.
FOR I = 1 TO Max
GET #1, I, FileBuffer
IF FileBuffer.NameField = "Tom Tucker" THEN
ReWriteFlag = TRUE
EXIT FOR
END IF
NEXT I
IF ReWriteFlag = TRUE THEN
'Back up file by the length of the record variable that
'is used to write to the file.
FileBuffer.ScoreField = 100
SEEK #1, SEEK(1) - LEN(RecordVar)
PUT #1, , RecordVar
END IF
CLOSE #1
KILL "TESTDAT2.DAT"
END