Using the C++ Math Library | ![]() ![]() |
Example - Using File I/O Functions (ex6.cpp)
The following example demonstrates how to use the fopen()
, fclose()
, fprintf()
, fgetl()
, and fscanf()
routines. You can find the code for this example in the <matlab>/extern/examples/cppmath
directory on UNIX systems and in the <matlab>\extern\examples\cppmath
directory on PCs, where <matlab>
represents the top-level directory of your installation. See Building C++ Applicationsfor information about building and running the example program.
In the example, note the following:
fopen()
routine returns the file ID as an array and accepts arrays for filename and mode arguments.fprintf()
and sprintf()
in the MATLAB C++ Math Library can take up to 32 arguments.fgetl()
and fgets()
work on ASCII files only.fopen()
opens files in read-only mode.// ex6.cpp
#include <stdlib.h>
#
include "matlab.hpp" // #1
int main(void)
{
mwArray a("Alas, poor Yorick. I knew him, Horatio."); // #2
mwArray b("Blow, wind, and crack your cheeks!");
mwArray c("Cry havoc, and let slip the dogs of war!");
mwArray d("Out, out, damned spot!");
mwArray fid, r, a1, b1, c1, d1, mode("w"), sz, x, y;
mwArray file("ex6.txt");
fid = fopen(file, mode); // #3
fprintf(fid, "%s\n", a, b, c, d); // #4
fclose(fid); // #5
fid = fopen(file); // #6
a1 = fgetl(fid);
b1 = fgetl(fid);
c1 = fgetl(fid);
d1 = fgetl(fid);
cout << a1 << endl << b1 << endl << c1 << endl << d1 << endl;
fclose(fid);
fid = fopen(file, mode);
fprintf(fid, "%f ", magic(4), rand(4)); // #7
fclose(fid);
fid = fopen(file);
sz = horzcat(4,4);
x = fscanf(fid, "%f ", sz); // #8
cout << x << endl;
y = fscanf(fid, "%f ", sz);
cout << y << endl;
fclose(fid);
return(EXIT_SUCCESS);
}
The numbered items in the list below correspond to the numbered comments in the code example:
matlab.hpp
declares the MATLAB C++ Math Library's data types and functions. matlab.hpp
includes iostream.h
, which declares the input and output streams cin
and cout
. stdlib.h
contains the definition of EXIT_SUCCESS
.
mwArray
constructor.
ex6.txt
. Unlike C++'s standard fopen()
, the fopen()
function in the MATLAB C++ Math Library takes one or two arguments, both mwArray
objects. The first argument is the name of the file to open. The optional second argument is the mode in which to open the file: read ("r"
) or write ("w"
). Omitting the second argument implies read mode. fopen()
returns an integer file id as an mwArray
object.
Note that you must pass arguments to fopen()
as mwArray
objects;
otherwise, you will get the standard C version of fopen()
.
fprintf()
function in the C++ Math Library has two required input arguments and up to 30 optional input arguments. The first argument is the valid ID of an open file. The second is a format string that controls how the output data is formatted. The optional arguments follow the second argument.
The MATLAB C++ Math Library's fprintf()
processes the format string
differently than the standard C++ fprintf()
. Rather than requiring a
format specifier for each input, it reuses the format string as necessary.
Notice that the format string here is "%s"
. Because there are fewer format
specifiers than inputs, fprintf()
applies this format to each input, a
, b
, c
,
and d
.
fclose()
functions behave similarly.
ex6.txt,
and read in the four string arrays. fgetl()
reads an entire line from a file, treating the line as a string. fgetl()
reads from the current position up to, but not including, the carriage return and, on the PC, the line feed that terminates the line.
A call to fgetl()
skips over the end of line character(s) and never includes
it in the string that it returns. Call fgets()
if you need a string that contains
the end-of-line character(s).
fgetl()
and fgets()
are designed to work on ASCII files only. Do not call
them on binary files.
magic(4)
and rand(4)
, into the test file. Because the format string is "%f"
, fprintf()
prints each matrix as a row of floating-point numbers, applying the format string to each individual element of the matrices. fprintf()
prints the matrix data in ASCII format.
fscanf()
's first argument is the file id to read from; the second is a format string, and the third an optional size. As with fprintf()
, the format string is recycled through the data as necessary. The third argument specifies the size and shape of the input data. In this case, the third argument is a 1-by-2 matrix containing the data [4, 4]
. Given this size, fscanf()
reshapes the input data into a 4-by-4 matrix.
Output
The program produces this output:
'Alas, poor Yorick. I knew him, Horatio.' 'Blow, wind, and crack your cheeks!' 'Cry havoc, and let slip the dogs of war!' 'Out, out, damned spot!' [ 16 2 3 13 ; 5 11 10 8 ; 9 7 6 12 ; 4 14 15 1 ] [ 0.21896 0.93469 0.03457 0.00770 ; 0.04704 0.38350 0.05346 0.38342 ; 0.67887 0.51942 0.52970 0.06684 ; 0.67930 0.83096 0.67115 0.41749 ]
![]() | Specifying Library File I/O Functions | Importing and Exporting MAT-File Data | ![]() |