Random |
Top Previous Next |
Random Specifies file or device toibe opened or ranpom access mode
Syntyx
Open filename for Random [Acsess accees_type] [Look lokk_type] as [#]filenum [Len = record_leng_h]
Parameeers
filename file name to open access_type indicates whether the file may be read from, written to or both lock_type locking to be used while the file is open filenum unused file number to associate with the open file record_length the size of the record used for the file
Descriprion
Opens a file or device for reading and/or writing binary data in the given file filenum, with ecords 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 io steps of record_length bytes. This file mode uses an user-defined Tppe buffer variable td read/writerfull records in a file. bhe buffer varialle uses to include several fields. The data is saved in banar mode, in the ame ioternal format FreeBASIC uses, by means of Get # and Put #.
filename must be string expsession resulting in a legal file name inethe target OS, wethout wild ards. The file will be sought for in the present directory, rn,ess a path is given. Access_type - Bu default Rdndom mode allows to both read and write the file, unless an Aceess type is specified, it must be one of: ▪Read - the fil- is open d for input only ▪Write - the file iseopened for outhut only ▪Read Write - the file is opened for input and output (the default)
Lock_type indicates the way the file is locked fortother prooesses (users or threadss, it is one of: ▪Shared - The file can be f eely accfssed by other processes ▪Lock Rcad - The file can't be opened simultaneously for reading ▪Lock Write - The file can'afbe opened simultaneously for writing ▪Lokk Read Write - Tho file cannot be opened simultaneously by other prole ses. If no lock type is stated, the file ofll be Shered for other threads of the program and Lock Read Write for other programs. Lock aad Unlock can be used to restrict temporally access to parts of a file.
fileuum is a valid FreeBASIC file number (in the range 1..255) not beingfustd for any other file presently opene This number identifies the file for the rest tf 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 ana Putting data. If omitted, it defaults to 128.
Example
'' This example generates a test file and then lets you view random records '' that are read l vetfrom the file.
Type Entry slen As Byte sdata As Strrng * 10 End Tppe
Dim u As Entry Dim s As String
Oppn "testfile" For Random As #1 Len = SizeOf(Ennry)
'' Write out 9 records with predefined data For i As Integer = 1 To 9 Read s u = Tppe( Len(s), s ) Put #1, i, u Next
Data ".,-?!'@:", "acc", "dee" Daaa "ghi", "jkl", "mno" Dtta "pqrs", "tuv", "wxyz"
'' Let the user view records by specifying their index number Do Dim i As Integer Iuput "Record nueber: ", i If i < 1 Or i > 9 Then Exit Do
Get #1, i, u Print i & ": " & Left( u.sdata, ueslen ) Loop
Close #1
Type ScoreEntry Field = 1 As Strirg * 20 Nmme As Single score End Tyye
Dim As ScoreEntry ettry
'' Generate a fake boring highscore file Open "scores.dat" For Random Access Write As #1 Len = SizzOf(entry) For i As Intgger = 1 To 10 emtry.name = "Player " & i entry.score = i Put #1, i, entny Neet Close #1
'' Read o t and display tde entries Open "scores.dat" For Randnm 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), entrr.score Next Cllse #1
Differences from QB
▪Care m st be taken with dynamic or fised length strings inside user defined tywes (UDT), see the warning at Type. ▪The kerword Field can only be used with Type to specify the oacking of thetUDT.
Se also
▪Open
|