REWIND statement

Purpose:

REWIND moves the read/write pointer of a file to the beginning of the file without the need to close and then re-open the file.

Syntax:

REWIND(FileHandle AS FILE)

Parameters:

  • Data type: FILE
    FileHandle The handle of the file in which the read/write pointer is to be moved.

Example:

 DIM A_Line$
 OPEN "Test.txt" FOR OUTPUT AS FP0
 FPRINT FP0, "This is the first line of Test.txt."
 FPRINT FP0, "This is the second line of Test.txt."
 FPRINT FP0, "This is the third line of Test.txt."
 FPRINT FP0, "This is the fourth line of Test.txt."
 CLOSE FP0
   
 OPEN "Test.txt" FOR INPUT AS FP1
 A_Line$ = LOOKAHEAD$(FP1, 0)
 PRINT A_Line$
 FOR INTEGER j = 1 TO 4
   LINE INPUT FP1, A_Line$
   PRINT A_Line$
 NEXT

 ?
 PRINT "Again, from the beginning !"
 REWIND(FP1) '<<< Reset File Pointer  
 LINE INPUT FP1, A_Line$
 ?
 PRINT A_Line$
 A_Line$ = LOOKAHEAD$(FP1, 3)
 PRINT A_Line$
 CLOSE FP1

 PAUSE

Result:

This is the first line of Test.txt.
This is the second line of Test.txt.
This is the third line of Test.txt.
This is the fourth line of Test.txt.

Again, from the beginning !

This is the first line of Test.txt.
This is the fourth line of Test.txt.

Press any key to continue . . .

LOOKAHEAD$ statement

Purpose:

LOOKAHEAD$ will return the next, or an optionally specified, line of text from an open file stream without permanently changing the current line pointer.

Syntax:

A_Line = LOOKAHEAD$(FileHandle AS FILE, [, LineToRead AS INTEGER])

Return Value:

  • Data type: STRING
    A_Line A line of text from an open file stream.

Parameters:

  • Data type: FILE
    FileHandle The handle of the file in which the read/write pointer is to be moved.
  • Data type: INTEGER
    LineToRead The ordinal numeral specifying which line to return. The default is 1, which specifies the next line, that is, the 1st line following the position of the current line pointer.

Example:

OPEN "Test.txt" FOR OUTPUT AS FP1

FPRINT FP1, "This is the first line of Test.txt."
FPRINT FP1, "This is the second line of Test.txt."
FPRINT FP1, "This is the third line of Test.txt."
FPRINT FP1, "This is the fourth line of Test.txt."

CLOSE FP1

OPEN "Test.txt" FOR INPUT AS FP1

PRINT LOOKAHEAD$(FP1, 4)
PRINT LOOKAHEAD$(FP1, 3)
PRINT LOOKAHEAD$(FP1, 2)
PRINT LOOKAHEAD$(FP1, 1)

CLOSE FP1
KILL "Test.txt"
PAUSE

Result:

This is the fourth line of Test.txt.
This is the third line of Test.txt.
This is the second line of Test.txt.
This is the first line of Test.txt.

Press any key to continue . . .