Using the C++ Math Library | ![]() ![]() |
Using Stream I/O to Files
The preceding example demonstrates stream input from and output to the terminal. To read or write arrays from and to files, use the C++ class ifstream
to create file input streams and ofstream
to create file output streams.
For example, the code fragment shown below writes array A
to a file and then reads the data from the file into array B
.
Note that in order to run the code fragment, you need to insert the following at the top of the program:
#include <fstream.h>
The code fragment is as follows:
mwArray A = rand(5), B; ofstream out_file("junk.txt", ios::out); out_file << A << ends; out_file.close(); ifstream in_file("junk.txt", ios::in); in_file >> B;
![]() | Stream I/O Format Definitions | Using Streams for Interprocess Communication | ![]() |