[651] | 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"> |
---|
[261] | 3 | <title>mixpp: Writing and reading data from files</title> |
---|
[651] | 4 | <link href="tabs.css" rel="stylesheet" type="text/css"> |
---|
| 5 | <link href="doxygen.css" rel="stylesheet" type="text/css"> |
---|
| 6 | </head><body> |
---|
| 7 | <!-- Generated by Doxygen 1.5.9 --> |
---|
[271] | 8 | <script type="text/javascript"> |
---|
| 9 | <!-- |
---|
| 10 | function changeDisplayState (e){ |
---|
| 11 | var num=this.id.replace(/[^[0-9]/g,''); |
---|
| 12 | var button=this.firstChild; |
---|
| 13 | var sectionDiv=document.getElementById('dynsection'+num); |
---|
| 14 | if (sectionDiv.style.display=='none'||sectionDiv.style.display==''){ |
---|
| 15 | sectionDiv.style.display='block'; |
---|
| 16 | button.src='open.gif'; |
---|
| 17 | }else{ |
---|
| 18 | sectionDiv.style.display='none'; |
---|
| 19 | button.src='closed.gif'; |
---|
| 20 | } |
---|
| 21 | } |
---|
| 22 | function initDynSections(){ |
---|
| 23 | var divs=document.getElementsByTagName('div'); |
---|
| 24 | var sectionCounter=1; |
---|
| 25 | for(var i=0;i<divs.length-1;i++){ |
---|
| 26 | if(divs[i].className=='dynheader'&&divs[i+1].className=='dynsection'){ |
---|
| 27 | var header=divs[i]; |
---|
| 28 | var section=divs[i+1]; |
---|
| 29 | var button=header.firstChild; |
---|
| 30 | if (button!='IMG'){ |
---|
| 31 | divs[i].insertBefore(document.createTextNode(' '),divs[i].firstChild); |
---|
| 32 | button=document.createElement('img'); |
---|
| 33 | divs[i].insertBefore(button,divs[i].firstChild); |
---|
| 34 | } |
---|
| 35 | header.style.cursor='pointer'; |
---|
| 36 | header.onclick=changeDisplayState; |
---|
| 37 | header.id='dynheader'+sectionCounter; |
---|
| 38 | button.src='closed.gif'; |
---|
| 39 | section.id='dynsection'+sectionCounter; |
---|
| 40 | section.style.display='none'; |
---|
| 41 | section.style.marginLeft='14px'; |
---|
| 42 | sectionCounter++; |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | window.onload = initDynSections; |
---|
| 47 | --> |
---|
| 48 | </script> |
---|
[261] | 49 | <div class="navigation" id="top"> |
---|
| 50 | <div class="tabs"> |
---|
| 51 | <ul> |
---|
[271] | 52 | <li><a href="main.html"><span>Main Page</span></a></li> |
---|
[290] | 53 | <li class="current"><a href="pages.html"><span>Related Pages</span></a></li> |
---|
| 54 | <li><a href="annotated.html"><span>Classes</span></a></li> |
---|
[261] | 55 | <li><a href="files.html"><span>Files</span></a></li> |
---|
| 56 | </ul> |
---|
| 57 | </div> |
---|
[632] | 58 | <div class="navpath"><a class="el" href="dev_guide.html">BDM Use - in C++</a> |
---|
[271] | 59 | </div> |
---|
[261] | 60 | </div> |
---|
| 61 | <div class="contents"> |
---|
[651] | 62 | <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> |
---|
[354] | 63 | <div class="fragment"><pre class="fragment"><span class="preprocessor">#include <itpp/itcomm.h></span> |
---|
[261] | 64 | |
---|
| 65 | <span class="keyword">using namespace </span>itpp; |
---|
| 66 | |
---|
[538] | 67 | <span class="keywordtype">int</span> main() { |
---|
| 68 | <span class="comment">// Declare the it_file class</span> |
---|
| 69 | it_file ff; |
---|
[261] | 70 | |
---|
[651] | 71 | <span class="comment">// Open a file with the name "it_file_test.it"</span> |
---|
| 72 | ff.open ( <span class="stringliteral">"it_file_test.it"</span> ); |
---|
[261] | 73 | |
---|
[538] | 74 | <span class="comment">// Create some data to put into the file</span> |
---|
| 75 | vec a = linspace ( 1, 20, 20 ); |
---|
[261] | 76 | |
---|
[651] | 77 | <span class="comment">// Put the variable a into the file. The Name("a") tells the file class</span> |
---|
| 78 | <span class="comment">// that the next variable shall be named "a".</span> |
---|
| 79 | ff << Name ( <span class="stringliteral">"a"</span> ) << a; |
---|
[261] | 80 | |
---|
[538] | 81 | <span class="comment">// Force the file to be written to disc. This is useful when performing</span> |
---|
| 82 | <span class="comment">// iterations and ensures that the information is not stored in any cache</span> |
---|
| 83 | <span class="comment">// memory. In this simple example it is not necessary to flush the file.</span> |
---|
| 84 | ff.flush(); |
---|
[261] | 85 | |
---|
[538] | 86 | <span class="comment">// Close the file</span> |
---|
| 87 | ff.close(); |
---|
[261] | 88 | |
---|
[538] | 89 | <span class="comment">// Exit program</span> |
---|
| 90 | <span class="keywordflow">return</span> 0; |
---|
[261] | 91 | } |
---|
[651] | 92 | </pre></div><p> |
---|
| 93 | 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> |
---|
| 94 | <div class="fragment"><pre class="fragment">itload(<span class="stringliteral">'it_file_test.it'</span>) |
---|
[261] | 95 | figure(1); clf; |
---|
| 96 | plot(a) |
---|
[651] | 97 | </pre></div><p> |
---|
| 98 | 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> |
---|
| 99 | The IT++ program <code>read_it_file.cpp</code> that reads the file and prints its content can look like this:<p> |
---|
| 100 | <div class="fragment"><pre class="fragment"></pre></div><p> |
---|
| 101 | Here is the output of the program:<p> |
---|
[261] | 102 | <div class="fragment"><pre class="fragment"> |
---|
| 103 | a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] |
---|
| 104 | </pre></div> </div> |
---|
[651] | 105 | <hr size="1"><address style="text-align: right;"><small>Generated on Wed Oct 7 17:34:44 2009 for mixpp by |
---|
[261] | 106 | <a href="http://www.doxygen.org/index.html"> |
---|
[651] | 107 | <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address> |
---|
[261] | 108 | </body> |
---|
| 109 | </html> |
---|