SEEK: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
The '''SEEK''' function returns the current byte or record position in a file.
The '''SEEK''' statement sets the byte or record position in a file for the next read or write.




{{PageSyntax}}
: [[SEEK]] {{Parameter|filenumber&}}, {{Parameter|position}}
{{PageParameters}}
* {{Parameter|filenumber&}} must be the file number that is [[OPEN]] and being read or written to.
* {{Parameter|position}} is a byte in [[BINARY]] or sequencial files created in [[OUTPUT]], [[APPEND]] or [[INPUT (file mode)|INPUT]] modes. The first byte = 1.
* {{Parameter|position}} is the record in [[RANDOM]] files to read or write. Records can hold more than one variable defined in a [[TYPE]].
* Since the first '''SEEK''' file position is 1 it may require adding one to an offset value when documentation uses that position as 0.
* After a '''SEEK''' statement, the next file operation starts at that '''SEEK''' byte position.
* The '''SEEK''' statement can work with the [[SEEK (function)]] to move around in a file.
=== Notes ===
* Don't confuse the [[LOC]] position with the [[SEEK (function)|SEEK]] position !!
** '''LOC''' is the {{Text|last|red}} read or written byte or record prosition.
** '''SEEK''' is the byte or record prosition to read or write {{Text|next|red}}.
{{PageExamples}}
;Example 1:A '''SEEK''' statement using the [[SEEK (function)]] to move to the next random record in a file.
{{CodeStart}}
{{Cl|SEEK}} 1, {{Cl|SEEK (function)|SEEK}}(1) + 1
{{CodeEnd}}


{{PageSyntax}}
----
:: byte = SEEK(filenumber&)
 
;Example 2:Demonstrate the difference between '''LOC''' and '''SEEK''' positions in a file.
{{CodeStart}}
OPEN "readme.md" FOR BINARY AS #1
 
PRINT LOC(1) 'LOC returns 0, as we didn't read something yet
PRINT SEEK(1) 'SEEK otherwise returns 1, as it's the first byte to read
 
GET #1, , a& 'now let's read a LONG (4 bytes)


PRINT LOC(1) 'now LOC returns 4, the last read byte
PRINT SEEK(1) 'and SEEK returns 5 now, the next byte to read


* Filenumber is the number of an [[OPEN]] file in any mode.
CLOSE #1
* In [[RANDOM]] files SEEK returns the current record position.
END
* In [[BINARY]] or sequencial files SEEK returns the current byte position(first byte = 1).
{{CodeEnd}}
* Since the first file position is 1 it may require adding one to an offset value when documentation uses that position as 0.
{{OutputStart}}
* Devices that do not support SEEK (SCRN, CONS, KBRD, COMn and LPTn) return 0.
0
1
4
5
{{OutputEnd}}




''See also:''
{{PageSeeAlso}}
*[[SEEK (statement)]]
* [[SEEK (function)]], [[LOC]]
*[[LOC]]
* [[GET]], [[PUT]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 00:10, 25 February 2023

The SEEK statement sets the byte or record position in a file for the next read or write.


Syntax

SEEK filenumber&, position


Parameters

  • filenumber& must be the file number that is OPEN and being read or written to.
  • position is a byte in BINARY or sequencial files created in OUTPUT, APPEND or INPUT modes. The first byte = 1.
  • position is the record in RANDOM files to read or write. Records can hold more than one variable defined in a TYPE.
  • Since the first SEEK file position is 1 it may require adding one to an offset value when documentation uses that position as 0.
  • After a SEEK statement, the next file operation starts at that SEEK byte position.
  • The SEEK statement can work with the SEEK (function) to move around in a file.

Notes

  • Don't confuse the LOC position with the SEEK position !!
    • LOC is the last read or written byte or record prosition.
    • SEEK is the byte or record prosition to read or write next.


Examples

Example 1
A SEEK statement using the SEEK (function) to move to the next random record in a file.
SEEK 1, SEEK(1) + 1

Example 2
Demonstrate the difference between LOC and SEEK positions in a file.
OPEN "readme.md" FOR BINARY AS #1

PRINT LOC(1) 'LOC returns 0, as we didn't read something yet
PRINT SEEK(1) 'SEEK otherwise returns 1, as it's the first byte to read

GET #1, , a& 'now let's read a LONG (4 bytes)

PRINT LOC(1) 'now LOC returns 4, the last read byte
PRINT SEEK(1) 'and SEEK returns 5 now, the next byte to read

CLOSE #1
END
0
1
4
5


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link