Repository navigation
Random
Home › File I/O Functions › Random
Opens a file with Open for reading and writing fixed-size records, 128 bytes by default.
Open filename for Random [Access access_type] [Lock lock_type] as [#]filenum [Len = record_length]filenameaccess_typelock_typefilenumrecord_length
Opens a file or device for reading and/or writing binary data in the given file filenum, with records of size record_length.
If the file does not exist, a new file will be created, otherwise any data existing in the file is preserved by Open. The file pointer is initialized by Open at the start of the file, at record number 1. File operations move the file position in steps of record_length bytes.
This file mode uses an user-defined Type buffer variable to read/write full records in a file. The buffer variable uses to include several fields.
The data is saved in binary mode, in the same internal format FreeBASIC uses, by means of Get # and Put #.
filename must be string expression resulting in a legal file name in the target OS, without wildcards. The file will be sought for in the present directory, unless a path is given.
Access_type - By default Random mode allows to both read and write the file, unless an Access type is specified, it must be one of:
Lock_type indicates the way the file is locked for other processes (users or threads), it is one of:
If no lock type is stated, the file will be Shared for other threads of the program and Lock Read Write for other programs.
Lock and Unlock can be used to restrict temporally access to parts of a file.
filenum is a valid FreeBASIC file number (in the range 1..255) not being used for any other file presently open. This number identifies the file for the rest of file operations. A free file number can be found using the FreeFile function.
record_length is the amount of bytes the file pointer will move for each individual Get and Put, it must match the size of the buffer variable used when Getting and Putting data. If omitted, it defaults to 128.
- Care must be taken with dynamic or fixed length strings inside user defined types (UDT), see the warning at Type.
The keyword
Fieldcan only be used withTypeto specify the packing of the UDT.
'' This example generates a test file and then lets you view random records
'' that are read live from the file.
Type Entry
slen As Byte
sdata As String * 10
End Type
Dim u As Entry
Dim s As String
Open "testfile" For Random As #1 Len = SizeOf(Entry)
'' Write out 9 records with predefined data
For i As Integer = 1 To 9
Read s
u = Type( Len(s), s )
Put #1, i, u
Next
Data ".,-?!'@:", "abc", "def"
Data "ghi", "jkl", "mno"
Data "pqrs", "tuv", "wxyz"
'' Let the user view records by specifying their index number
Do
Dim i As Integer
Input "Record number: ", i
If i < 1 Or i > 9 Then Exit Do
Get #1, i, u
Print i & ": " & Left( u.sdata, u.slen )
Print
Loop
Close #1Type ScoreEntry Field = 1
As String * 20 Name
As Single score
End Type
Dim As ScoreEntry entry
'' Generate a fake boring highscore file
Open "scores.dat" For Random Access Write As #1 Len = SizeOf(entry)
For i As Integer = 1 To 10
entry.name = "Player " & i
entry.score = i
Put #1, i, entry
Next
Close #1
'' Read out and display the entries
Open "scores.dat" For Random Access Read As #1 Len = SizeOf(entry)
For i As Integer = 1 To 10
Get #1, i, entry
Print i & ":", entry.name, Str(entry.score), entry.score
Next
Close #1Category: File I/O Functions
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
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
- Partial Types
- 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