Maple Professional
Maple Academic
Maple Student Edition
Maple Personal Edition
Maple Player
Maple Player for iPad
MapleSim Professional
MapleSim Academic
Maple T.A. - Testing & Assessment
Maple T.A. MAA Placement Test Suite
Möbius - Online Courseware
Machine Design / Industrial Automation
Aerospace
Vehicle Engineering
Robotics
Power Industries
System Simulation and Analysis
Model development for HIL
Plant Modeling for Control Design
Robotics/Motion Control/Mechatronics
Other Application Areas
Mathematics Education
Engineering Education
High Schools & Two-Year Colleges
Testing & Assessment
Students
Financial Modeling
Operations Research
High Performance Computing
Physics
Live Webinars
Recorded Webinars
Upcoming Events
MaplePrimes
Maplesoft Blog
Maplesoft Membership
Maple Ambassador Program
MapleCloud
Technical Whitepapers
E-Mail Newsletters
Maple Books
Math Matters
Application Center
MapleSim Model Gallery
User Case Studies
Exploring Engineering Fundamentals
Teaching Concepts with Maple
Maplesoft Welcome Center
Teacher Resource Center
Student Help Center
fopen - opens a file for buffered reading or writing
Calling Sequence
fopen(name, mode)
fopen(name, mode, type)
Parameters
name
-
the name of the file to be opened
mode
one of READ, WRITE, or APPEND
type
optional, one of TEXT or BINARY
Description
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.
Thread Safety
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.
Examples
Use of 'terminal' for output
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