Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Handle Files with C++

The mexatexit.cpp example shows C++ file handling features. Compare it with the C code example mexatexit.c, which uses the mexAtExit function.

C++ Example

The C++ example uses a fileresource class to handle the file open and close functions. The MEX file calls the destructor for this class (which closes the data file). This example also prints a message on the screen when performing operations on the data file. However, in this case, the only C file operation performed is the write operation, fprintf.

To build the mexatexit.cpp MEX file, type:

mex mexatexit.cpp

Type:

z = 'for the C++ MEX-file';
mexatexit(x)
mexatexit(z)
clear mexatexit
Writing data to file.
Writing data to file.

Display the contents of matlab.data.

type matlab.data
my input string
for the C++ MEX-file

C Example

The C code example registers the mexAtExit function to perform cleanup tasks (close the data file) when the MEX file clears. This example prints a message on the screen (using mexPrintf) when performing file operations fopen, fprintf, and fclose.

To build the MEX file, type:

mex mexatexit.c

Run the example.

x = 'my input string';
mexatexit(x)
Opening file matlab.data.
Writing data to file.

Clear the MEX file.

clear mexatexit
Closing file matlab.data.

Display the contents of matlab.data.

type matlab.data
my input string

See Also

Related Examples

Was this topic helpful?