Q(uick)BASIC Function: LOC

Quick View

LOC

A file I/O function that returns the current position within the file

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • LOC(filenumber%)
Description/Parameter(s)
filenumber% The number of an open file or device.
  • For binary files, LOC returns the position of the last byte read or written.
  • For random-access files, LOC returns the number of the last record read from or written to the file.
  • For sequential files, LOC returns the current byte position in the file, divided by 128.
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)
Syntax
  • LOC(filenumber)
Description/Parameter(s)

The filenumber is the number used in the OPEN statement to open the file. With random-access files, the LOC function returns the number of the last record read from or written to the file. With sequential files, LOC returns the current byte position in the file, divided by 128. With binary mode files, LOC returns the position of the last byte read or written.

For a COM device, LOC(filenumber) returns the number of characters in the input queue waiting to be read. The value returned depends on whether the device was opened in ASCII or binary mode. In ASCII mode, the low-level routines stop queuing characters as soon as end-of-file is received. The end-of-file itself is not queued and cannot be read. An attempt to read the end-of-file produces an error message that reads "Input past end of file." In binary mode, the end-of-file character is ignored and the entire file can be read.

Example

This example stops the program if the current file position is beyond 50.
Note: This example is incomplete. Do not run this example in its current form.

IF LOC(1) > 50 THEN STOP
Syntax
  • LOC(filenumber%)
Description/Parameter(s)

Usage Notes

  • For a COM device, LOC returns the number of characters in the input queue waiting to be read. The value returned depends on whether the device was opened in ASCII or binary mode.
  • In ASCII mode, the low-level routines stop queuing characters as soon as end-of-file is received. The end-of-file itself is not queued and cannot be read. If you attempt to read the end-of-file, BASIC generates the error message, "Input past end of file."
  • In binary mode, the end-of-file character is ignored and the entire file can be read.
  • For a PIPE device, LOC returns 1 if any data is available in the PIPE queue.

Important

  • The LOC function cannot be used on ISAM tables, or the SCRN, KYBD, or LPTn devices.
Example

This example uses the LOC function to stop the program if the current file position is beyond 50.
Note: This example is incomplete.

IF LOC(1) > 50 THEN STOP