Repository navigation
Modern Fs RecordFile
Paul Squires edited this page Oct 24, 2026
·
1 revision
Home › Filesystem › File Streams › RecordFile
Holds a file of fixed-size records of one type, read and written by number.
type RecordFile( of T )
Open( p [, recordLen ] ) / Open( p, opts [, recordLen ] )
GetRecord( n, r ) / PutRecord( n, r ) / Append( r ) / Count( )
Close( ) / IsOpen( ) / RecordLength( )
for each r in rf '' each an FsResult of T
end type- Records count from 1. Record n is at ( n - 1 ) * RecordLength( ), which is sizeof( T ) unless Open was given a longer length for a file whose records are padded.
- Reading a record that is not all there, past the end or a short last record, is Ok( false ) with the record untouched, and Count( ) does not count a short record. Writing past the end grows the file; the records in the gap read as zeros.
- T must be plain data: numbers, zstring * n and wstring * n, and types made of those. A ustring, string, pointer or dynamic array is written as the address it holds, which means nothing when it is read back, and nothing checks this for you. Add field = 1 to T for a layout with no padding.
- for each walks records 1, 2, ... while they are whole, yielding Ok( record ) for each. An IO failure is yielded once, as a failure, and ends the walk.
- Close( ) closes the file early; it closes itself when the RecordFile goes out of scope. IsOpen( ) says whether it is open. RecordLength( ) is the length in bytes of one record.
- It owns its File, so do not copy a RecordFile: pass it byref.
- No record locks: File.Lock locks the whole file.
- Underneath are ReadRecord, WriteRecord and RecordCount, which work on a File you opened yourself.
#import "std/fs"
using fs
type Score field = 1
player as long
points as long
end type
dim as RecordFile( of Score ) rf
rf.Open( "scores.dat" )
for n as long = 1 to 3
dim as Score s
s.player = n : s.points = n * 10
rf.Append( s )
next
for each r in rf
print r.Value( ).player; r.Value( ).points
next
rf.Close( )
RemoveFile( "scores.dat" )
'' 1 10
'' 2 20
'' 3 30Library: Filesystem Library Category: File Streams Documented under: Records
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing