Q(uick)BASIC Statement: SEEK
Quick View
SEEK
A file I/O statement that sets the position in a file for the next read or write
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,position
Description/Parameter(s)
The filenumber is an integer number used in the OPEN statement to open the file.
The position is a numeric expression indicating where the next read or write is done. The position must be in the range 1 to 2,147,483,647 (equivalent to 2^31 -1). For files opened in RANDOM mode, position is the number of a record in the file.
For files opened in BINARY, INPUT, OUTPUT, or APPEND modes, position is the number of a byte from the beginning of the file. The first byte in a file is 1. After a SEEK, the next file I/O operation starts at that byte in the file.
Note: | Record numbers on a GET or PUT override the file positioning done by SEEK. |
A SEEK to a negative or zero position produces an error message that reads "Bad record number." Performing a file write after doing a SEEK beyond the end of a file extends the file.
When used on a device that does not support SEEK, BASIC ignores SEEK and leaves the file position unchanged. The BASIC devices (SCRN:, CONS:, KYBD:, COMn:, and LPTn:) do not support SEEK.
Example
The following program uses a combination of the SEEK function and SEEK statement to move the file position exactly one record back and rewrite the record if a variable is true (nonzero).
'*** Programming example for the SEEK function and statement
'
CONST FALSE=0, TRUE=NOT FALSE
' Define record fields.
TYPE TestRecord
NameField AS STRING * 20
ScoreField AS SINGLE
END TYPE
' Define a variable of the user type.
DIM RecordVar AS TestRecord
'********************************************************************
' This part of the program is an insert whose only function is to
' create a random-access file to be used by the second part of the
' program, which demonstrates the CVSMBF function
'********************************************************************
OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN = LEN(Rec)
CLS
RESTORE
READ NameField$, ScoreField
I = 0
DO WHILE UCASE$(NameField$) <> "END"
I = I + 1
RecordVar.NameField = NameField$
RecordVar.ScoreField = ScoreField
PUT #1, I, RecordVar
READ NameField$, ScoreField
IF NameField$ = "END" THEN EXIT DO
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 and print 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
See also:
Syntax
- SEEK [#]filenumber%, position&
Description/Parameter(s)
filenumber% | The number used in the OPEN statement to open the file. | |||
position& | A number that indicates where the next read or write occurs: | |||
|
- The argument filenumber% is an integer used in the OPEN statement to open the file.
- The argument position& is a numeric expression that indicates where the next read or write occurs. The position must be between 1 and 2,147,483,647, inclusive (equivalent to 2^31 -1).
- The first byte in a file is at position 1; the second byte is at position 2, and so on. After a SEEK, the next file I/O operation starts at the specified byte position.
Important
- Record numbers on a GET or PUT override the file positioning done by SEEK.
Usage Notes
- If you attempt a SEEK operation to a negative or zero position, BASIC generates the error message, "Bad record number." Performing a file write after doing a SEEK beyond the end of a file extends the file.
- BASIC leaves the file position unchanged when you use SEEK on an ISAM table or on BASIC devices (SCRN, CONS, KYBD, COMn, LPTn, PIPE) that do not support SEEK.
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.
CONST FALSE = 0, TRUE = NOT FALSE
'Define record fields.
TYPE TestRecord
NameField AS STRING * 20
ScoreField AS SINGLE
END TYPE
'Define a variable of the user type.
DIM RecordVar AS TestRecord
DIM I AS LONG
'Note: This part of the program is an insert whose function is to
'create a random-access file to be used by the second part of the
'program, which demonstrates the SEEK function and 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