Hallo Leutz,
ein Benutzer hat mir diesen coolen Code zusammengestellt um eine Matrix aus der txt-Datei einzulesen. .
Wie kann ich diesen Code so modifizieren das ich mehrere Matrizen einlesen und abspeichern kann?
Einen Auszug aus der txt-Datei:
Vielen Dank im Voraus und allen fröhliche Weihnachten
Ciao
ein Benutzer hat mir diesen coolen Code zusammengestellt um eine Matrix aus der txt-Datei einzulesen. .
C++:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
void display(vector<vector<double>> &);
int main(void) {
ifstream ifs("double.txt");
if (!ifs) {
cerr << "Datei kann nicht geöffnet werden" << endl;
exit(-1);
}
vector<vector<double>> matrix;
while (ifs) {
string line;
getline(ifs, line);
istringstream iss(line);
vector<double> values;
istream_iterator<double>(iss);
copy(istream_iterator<double>(iss),
istream_iterator<double>(),
back_insert_iterator<vector<double> >(values));
if (values.size() > 0) matrix.push_back(values);
}
display(matrix);
return 0;
}
void display(vector<vector<double>> &mat) {
int z = mat.size();
int s = mat.at(0).size();
cout << "Anzahl der Zeilen : " << z << endl;
cout << "Anzahl der Spalten: " << s << '\n' << endl;
for(int i = 0; i < z; ++i) {
for(int j = 0; j < s; ++j)
cout << mat.at(i).at(j) <<'\t';
cout << '\n';
}
}
Wie kann ich diesen Code so modifizieren das ich mehrere Matrizen einlesen und abspeichern kann?
Einen Auszug aus der txt-Datei:
Code:
=beg=50700 (Element Connectivities)
20 25 31 32 26
21 27 2 33 28
22 28 33 34 29
23 29 34 35 30
24 30 35 36 31
25 31 36 3 32
=end=
=beg=50800 (Nodal Coordinates)
1 0.000000E+00 0.100000E+02
2 0.500000E+02 0.100000E+02
3 0.500000E+02 0.100000E+03
4 0.000000E+00 0.100000E+03
5 0.000000E+00 0.280000E+02
6 0.000000E+00 0.460000E+02
7 0.000000E+00 0.640000E+02
8 0.000000E+00 0.820000E+02
=end=
Vielen Dank im Voraus und allen fröhliche Weihnachten

Ciao