fopen
opens a file for buffered reading or writing
Calling Sequence
Parameters
Description
Thread Safety
Examples
fopen(name, mode)
fopen(name, mode, type)
name
-
the name of the file to be opened
mode
one of READ, WRITE, or APPEND
type
optional, one of TEXT or BINARY
Opens the file with the specified name for buffered reading or writing as specified by mode and returns a file descriptor (a small integer).
In most cases, it is not necessary to fopen a file in order to access it. The first operation performed on a file will open it if it is not already open. The fopen function is provided as a convenience to those porting code written in other languages (such as C) to Maple, and for those cases where it is desirable to override the default file type provided by the I/O functions.
If a type is specified, it indicates whether the file contains TEXT (a stream of characters) or BINARY (a stream of bytes). When opened as type TEXT, newline characters ("\n") are translated to the platform-appropriate representation when writing, and back to newline characters when reading.
If no type is specified, type TEXT is assumed.
On platforms (such as UNIX) where there is no distinction between text and binary files, a type specification is allowed, but superfluous.
The special file names 'default' and 'terminal' (symbols) refer to the current and top-level input or output streams.
If the file being opened is already open, fopen generates an error.
If the file is being opened for reading and does not exist, fopen generates an error.
If the file is being opened for writing and does not exist (and is not already open), it is created, if possible; otherwise, fopen generates an error. If the file does exist (and is not already open), it is truncated. Writing starts at the beginning of the file if WRITE was specified, and at the end of the existing file if APPEND was specified.
If the limit for the maximum number of simultaneously open files has been reached, fopen generates an error.
The number of open files allowed is system specific.
When writing many files, you must fclose() each of them to ensure that you do not run out of file handles.
For information on filenames, see file.
The fopen command is thread safe as of Maple 15.
Parallel calls to file i/o commands on the same file descriptor will be serialized in an arbitrary order. If you need the commands to execute in a particular order you must use Maple's synchronization tools to enforce this. See Threads:-Mutex.
For more information on thread safety, see index/threadsafe.
fd≔fopen⁡testFile,WRITE,BINARY
fd≔0
fprintf⁡fd,This is a test\n
15
fclose⁡fd
Use of 'terminal' for output
fd≔fopen⁡terminal,WRITE:
fprintf⁡fd,This is a test\n:
This is a test
Note: fclose cannot be used with the terminal file descriptor
Error, (in fclose) operation not allowed on `terminal` stream
See Also
fclose
file
file_types
IO_errors
open
Download Help Document