1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// Example program
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <cassert>
#include <numeric>
#include <fstream>
#include <iterator>
using namespace std;
// Save vector as matrix type in Octave (.mat) file.
template <typename T>
void save_vector_as_matrix( const std::string& name, const std::vector<T>& matrix, ofstream &FILE )
{
std::size_t columns;
columns = matrix.size();
std::string nameLine = "# name: ", typeLine = "# type: matrix";
std::string rowsLine = "# rows: 1", columnsLine = "# columns: ";
nameLine += name;
FILE << nameLine << endl;
FILE << typeLine << endl;
FILE << rowsLine << endl;
FILE << columnsLine << columns << endl;
for ( std::size_t column = 0; column < columns; column++ )
FILE << " " << matrix[column];
FILE << "\n\n" << endl;
} // end template save_vector_as_matrix
// Load matrix type from Octave (.mat) file into vector.
template <typename T>
void load_matrix_to_vector( std::vector<T>& arr, ifstream &FILE )
{
std::size_t columns;
std::string str, STR;
if ( !std::getline(FILE, str) )
{
std::cout << "\nOctaveInterface.h inconsistency:" << endl;
std::cout << "In function load_matrix_to_vector:" << endl;
std::cout << "Cannot read line from file." << endl;
exit( EXIT_FAILURE );
}
STR = "# type: matrix";
if ( str.compare(STR) == 0 )
{
if ( !std::getline(FILE, str) )
{
std::cout << "\nOctaveInterface.h inconsistency:" << endl;
std::cout << "In function load_matrix_to_vector:" << endl;
std::cout << "Cannot read line from file." << endl;
exit( EXIT_FAILURE );
}
STR = "# rows: 1";
if ( str.compare(STR) != 0 )
{
std::cout << "\nOctaveInterface.h inconsistency:" << endl;
std::cout << "In function load_matrix_to_vector:" << endl;
std::cout << "matrix type does not fulfill function conditions." << endl;
std::cout << "It must be: " << STR << endl;
std::cout << "Yet, it is: " << str << endl;
exit( EXIT_FAILURE );
}
str.resize(11);
FILE.read(&str[0],11);
STR = "# columns: ";
if ( str.compare(STR) == 0 )
{
FILE >> columns;
}
else
{
std::cout << "\nOctaveInterface.h inconsistency:" << endl;
std::cout << "In function load_matrix_to_vector:" << endl;
std::cout << "File corrupted." << endl;
exit( EXIT_FAILURE );
}
std::getline(FILE, str);
arr.resize(columns);
for ( std::size_t column = 0; column < columns; column++ )
FILE >> arr[column];
}
else
{
std::cout << "\nOctaveInterface.h inconsistency:" << endl;
std::cout << "In function load_matrix_to_vector:" << endl;
std::cout << "arr must be of type matrix." << endl;
exit( EXIT_FAILURE );
}
} // end template load_matrix_to_vector
int main()
{
std::vector<std::size_t> a = {0,1,2,3,4,5,6,7,8,9};
std::cout << "\na:\n";
for(const auto& s : a)
std::cout << s << " ";
std::cout << "\n";
// open a file in write mode.
ofstream outfile;
outfile.open("./a.mat", ios::out | ios::trunc);
// file preamble.
outfile << "# This is a file created by a test function" << endl;
outfile << "# Author: Dematties Dario Jesus." << endl;
outfile << "\n\n" << endl;
// saves afferentArrayDimensionality
save_vector_as_matrix("a", a, outfile);
// close the opened file.
outfile.close();
std::cout << "\nvector a saved\n";
a.clear();
assert(a.size() == 0);
bool check_a = false;
std::string str;
std::string STR;
// open a file in read mode.
ifstream infile;
infile.open("./a.mat", ios::in | std::ifstream::binary);
while ( std::getline(infile, str) ) {
STR = "# name: a";
if ( str.compare(STR) == 0 ) {
load_matrix_to_vector(a, infile);
check_a = true;
}
}
// close the opened file.
infile.close();
assert(check_a == true);
std::cout << "\na:\n";
for(const auto& s : a)
std::cout << s << " ";
std::cout << "\n";
return 0;
}
|