Blitz3D+ Command Reference

FilePos ( file_stream )

Parameters

file_stream - file handle returned by ReadFile, WriteFile or OpenFile

Description

Returns the current read/write position within a file.

The position is the offset in bytes from the start of the file, so 0 means the very first byte. It advances automatically as you read or write, and jumps when you call SeekFile.

FilePos and SeekFile together give you random access: you can note where a record starts, wander off, and come back to it later without re-reading the whole file. That is the backbone of things like a save file with a table of contents, or a custom archive holding many resources.

See also: SeekFile, OpenFile, FileSize.

Example

; FilePos Example
; ---------------

; Save a name, score and x position, watching the write position move
file=WriteFile("demo_savegame.dat")
Print "Position at start        : "+FilePos(file)
WriteString file,"Robo"
Print "After WriteString 'Robo' : "+FilePos(file)+"  (4-byte length + 4 characters)"
WriteInt file,11657
Print "After WriteInt 11657     : "+FilePos(file)+"  (+4 bytes)"
WriteFloat file,12.5
Print "After WriteFloat 12.5    : "+FilePos(file)+"  (+4 bytes)"
CloseFile file

; FilePos works while reading too
file=ReadFile("demo_savegame.dat")
name$=ReadString$(file)
Print "After reading the name   : "+FilePos(file)
CloseFile file

; Clean up the demo file
DeleteFile "demo_savegame.dat"
Print ""
Print "Deleted demo_savegame.dat"

Print ""
Print "Press any key to close the example"
WaitKey

End

Index