Using the C++ Math Library | ![]() ![]() |
Specifying Library File I/O Functions
Because the MATLAB C++ Math Library file I/O functions have the same name as their C++ counterparts and because the types of their arguments are so similar, you must be careful to make sure you're calling the correct one.
This is particularly important with fprintf()
. The type of the first argument to fprintf()
is all important: if it is an array, the system calls the MATLAB C++ Math Library function; if it is an integer, the system calls the standard C++ function. Consider this example:
mwArray file("foo.txt"), data=rand(4); int fd = fopen(file); fprintf(fd, "%f", data);
The system calls the standard C++ fprintf()
function because the first argument passed to fprintf()
is an integer. But this is almost certainly not what the author intended; the standard C++ fprintf()
uses the format string to determine how many arguments it has. In this case, it will think there is a single argument and the program will crash because the standard fprintf()
function does not understand mwArray
objects.
The MATLAB C++ Math Library version of sprintf()
requires that you pass an mwArray
as its second argument. The other arguments may be passed as character strings.
![]() | Using File I/O Functions | Example - Using File I/O Functions (ex6.cpp) | ![]() |