Repository navigation
Open
Home › File I/O Functions › Open
Opens a file on a file number for reading or writing text, bytes or records.
Open filename For Input [encoding_type] [lock_type] As [#]filenumber
Open filename For Output [encoding_type] [lock_type] As [#]filenumber
Open filename For Append [encoding_type] [lock_type] As [#]filenumber
Open filename For Binary [access_type] [lock_type] As [#]filenumber
Open filename For Random [access_type] [lock_type] As [#]filenumber [Len = record_length]filenameencoding_typeaccess_typelock_typefilenumberrecord_length
Opens a disk file for reading and/or writing. The file number file_num is bound to the file on disk, for use in subsequent file operations, such as Input # and Lock. The next available file number can be retrieved with FreeFile.
The Input, Output and Append file modes open disk files for sequential text I/O, useful for reading or writing plain text files:
The Input, Output and Append file modes also allow selection of a character encoding to be used when reading from or writing text to the disk file. ASCII or a Unicode encoding may be specified (see the description of the encoding_type parameter above).
The Binary and Random file modes open disk files for random-access reading or writing of arbitrary sized binary data:
By default, the Binary and Random file modes allow both reading and writing operations on the opened disk file, but this can be changed by specifying an access type (see the description for the access_type parameter above). When opening an existing file in Binary or Random file mode, and with the only Write access type specified, all data in the file are previously deleted at opening, before any other operation.
For any file mode, access to the opened disk file can be restricted or granted to other threads or programs by specifying a lock type (see the description for the lock_type parameter above). If no lock type is specified, other threads of the current program can freely open the disk file (Shared), while other programs cannot (Lock Read Write). Lock and Unlock can be used to temporarily restrict access to parts of a file.
The error code returned by Open can be checked using Err in the next line. The function version of Open returns directly the error code as a 32 bit Long.
In the first usage, Open() returns a 32 bit Long value: zero (0) on success and a non-zero error code otherwise.
result = Open( filename[,] For {Input|Output|Append}[,] As filenumber )orresult = Open( filename[,] For Binary[,] Access {Read|Write}[,] As filenumber )orresult = Open( filename[,] For Random[,] Access {Read|Write}[,] As filenumber [[,] Len = record_length] )orOpen filename For {Input|Output|Append} As filenumberorOpen filename For Binary Access {Read|Write} As filenumberorOpen filename For Random Access {Read|Write} As filenumber [Len = record_length]
- Linux requires the
filenamecase matches the real name of the file. Windows, DOS and a default macOS volume are case insensitive. Path separators in Linux and macOS are forward slashes/. Windows uses backward slashes\but it allows for forward slashes/. DOS uses backward slashes\.
On Windows, a file number used in a dynamic link library is not the same as an identical file number used in the main program. File numbers can not be passed or returned and then used between a DLL and an executable.
If you try to open a directory on Linux, theOpencommand will succeed.
- Using MS-DOS device names to open streams or hardware devices (
"LPT:","SCR:", etc.) is supported only in the -lang qb dialect; for other modes FreeBASIC's new composite keywords must be used: seeOpen Com,Open Cons,Open Err,Open Pipe,Open Lpt,Open Scrn.Opencan be called as a function that returns an error code.
- The -lang qb dialect supports the old GW-BASIC-style syntax
OPEN mode_string, #filenumber, filename [length]with mode_string being"I"for input,"O"for output,"A"for append,"R"for random,"B"for binary.
-
Err(and a list of error codes)Close
FreeFile
Open Cons,Open Err,Open Pipe,Open Lpt,Open Com,Open Scrn
' Create a string and fill it.
Dim buffer As String, f As Integer
buffer = "Hello World within a file."
' Find the first free file number.
f = FreeFile
' Open the file "file.ext" for binary usage, using the file number "f".
Open "file.ext" For Binary As #f
If Err>0 Then Print "Error opening the file":End
' Place our string inside the file, using number "f".
Put #f, , buffer
' Close all open files.
Close
' End the program. (Check the file "file.ext" upon running to see the output.)
End'OPEN A COM PORT
Open Com "COM1:9600,N,8,1" As #1
If Err>0 Then Print "The port could not be opened."
'COM1, 9600 BAUD, NO PARITY BIT, EIGHT DATA BITS, ONE STOP BIT'function version of OPEN
If Open("file.ext" For Binary Access Read As #1) = 0 Then
Print "Successfully opened file"
'' ...
Close #1
Else
Print "Error opening file"
End IfCategory: 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.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- 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)
- Time Zones
- 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