Repository navigation
Binary
Home › File I/O Functions › Binary
Opens a file with Open for reading and writing bytes at any position.
Open filename for Binary [Access access_type] [Lock lock_type] as [#]filenumfilenameaccess_typelock_typefilenum
Opens a file or device for reading and/or writing binary data in the file filenum, with free format.
If the file does not exist, a new file will be created. The file pointer is initialized by Open at byte no. 1.
Get # and Put # file operations move the file pointer according to the size of the data, the pointer can be set to any byte in the file.
The data existing in the file is preserved by Open.
This file mode can use any buffer variable to read/write data in the file.
The data is saved in binary mode, in the same internal format FreeBASIC uses, by means of Get # and Put #.
filename must be a 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 Binary 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 file number (in the range 1..255) not being used for any other file presently open. The file number identifies the file for the rest of file operations. A free file number can be found using the FreeFile function.
- None
'' Create a binary data file with one number in it
Dim x As Single = 17.164
Open "MyFile.Dat" For Binary As #1
'' put without a position setting will put from the last known file position
'' in this case, the very beginning of the file.
Put #1, , x
Close #1'' Now read the number from the file
Dim x As Single = 0
Open "MyFile.Dat" For Binary As #1
Get #1, , x
Close #1
Print x'' Read entire contents of a file to a string
Dim txt As String
Open "myfile.txt" For Binary Access Read As #1
If LOF(1) > 0 Then
'' our string has as many characters as the file has in bytes
txt = String(LOF(1), 0)
'' size of txt is known. entire string filled with file data
Get #1, , txt
End If
Close #1
Print txtCategory: 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