1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
2 | <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
---|
3 | <title>mixpp: Writing and reading data from files</title> |
---|
4 | <link href="doxygen.css" rel="stylesheet" type="text/css"> |
---|
5 | <link href="tabs.css" rel="stylesheet" type="text/css"> |
---|
6 | </head><body> |
---|
7 | <!-- Generated by Doxygen 1.5.6 --> |
---|
8 | <div class="navigation" id="top"> |
---|
9 | <div class="tabs"> |
---|
10 | <ul> |
---|
11 | <li><a href="index.html"><span>Main Page</span></a></li> |
---|
12 | <li><a href="pages.html"><span>Related Pages</span></a></li> |
---|
13 | <li><a href="modules.html"><span>Modules</span></a></li> |
---|
14 | <li><a href="namespaces.html"><span>Namespaces</span></a></li> |
---|
15 | <li><a href="classes.html"><span>Classes</span></a></li> |
---|
16 | <li><a href="files.html"><span>Files</span></a></li> |
---|
17 | </ul> |
---|
18 | </div> |
---|
19 | </div> |
---|
20 | <div class="contents"> |
---|
21 | <h1><a class="anchor" name="itfile">Writing and reading data from files </a></h1>Here we will use the <code>it_file</code> class to store some data. The program <code>write_it_file.cpp</code> looks as follows:<p> |
---|
22 | <div class="fragment"><pre class="fragment"><span class="preprocessor">#include <itpp/itcomm.h></span> |
---|
23 | |
---|
24 | <span class="keyword">using namespace </span>itpp; |
---|
25 | |
---|
26 | <span class="keywordtype">int</span> main() |
---|
27 | { |
---|
28 | <span class="comment">// Declare the it_file class</span> |
---|
29 | it_file ff; |
---|
30 | |
---|
31 | <span class="comment">// Open a file with the name "it_file_test.it"</span> |
---|
32 | ff.open(<span class="stringliteral">"it_file_test.it"</span>); |
---|
33 | |
---|
34 | <span class="comment">// Create some data to put into the file</span> |
---|
35 | vec a = linspace(1, 20, 20); |
---|
36 | |
---|
37 | <span class="comment">// Put the variable a into the file. The Name("a") tells the file class</span> |
---|
38 | <span class="comment">// that the next variable shall be named "a".</span> |
---|
39 | ff << Name(<span class="stringliteral">"a"</span>) << a; |
---|
40 | |
---|
41 | <span class="comment">// Force the file to be written to disc. This is useful when performing</span> |
---|
42 | <span class="comment">// iterations and ensures that the information is not stored in any cache</span> |
---|
43 | <span class="comment">// memory. In this simple example it is not necessary to flush the file.</span> |
---|
44 | ff.flush(); |
---|
45 | |
---|
46 | <span class="comment">// Close the file</span> |
---|
47 | ff.close(); |
---|
48 | |
---|
49 | <span class="comment">// Exit program</span> |
---|
50 | <span class="keywordflow">return</span> 0; |
---|
51 | } |
---|
52 | </pre></div><p> |
---|
53 | When you run this program you will obtain a file called <code>it_file_test.it</code> in your current directory. You can read the file into Matlab/Octave to view the data by using the following commands:<p> |
---|
54 | <div class="fragment"><pre class="fragment">itload(<span class="stringliteral">'it_file_test.it'</span>) |
---|
55 | figure(1); clf; |
---|
56 | plot(a) |
---|
57 | </pre></div><p> |
---|
58 | Note: Make sure that <code>$PREFIX/share/itpp</code> is in your Matlab/Octave path and that you run the code above from the directory where <code>it_file_test.it</code> is located (<code>$PREFIX</code> is the IT++ installation prefix; <code>/usr/local</code> by default).<p> |
---|
59 | The IT++ program <code>read_it_file.cpp</code> that reads the file and prints its content can look like this:<p> |
---|
60 | <div class="fragment"><pre class="fragment"></pre></div><p> |
---|
61 | Here is the output of the program:<p> |
---|
62 | <div class="fragment"><pre class="fragment"> |
---|
63 | a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] |
---|
64 | </pre></div> </div> |
---|
65 | <hr size="1"><address style="text-align: right;"><small>Generated on Fri Feb 6 15:29:47 2009 for mixpp by |
---|
66 | <a href="http://www.doxygen.org/index.html"> |
---|
67 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address> |
---|
68 | </body> |
---|
69 | </html> |
---|