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:

The numbered items in the list below correspond to the numbered comments in the code example:

  1. Include header files. 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.
  2. Declare local variables, including four string arrays. Notice that you can make a string array by passing a string to the mwArray constructor.
  3. Open and create a file named 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().

  1. Write the string arrays into the file. The 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.

  1. Close the file. The standard C++ and MATLAB C++ Math Library fclose() functions behave similarly.
  2. Reopen 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.

  1. Print two numeric matrices, 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.
  2. Read the numeric matrices back from the test file. 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:


 Specifying Library File I/O Functions Importing and Exporting MAT-File Data