#include<iostream>
#include<ostream>
#include<string>
#include<fstream>
usingnamespace std;
int main ()
{
double a[9]; // Space for a 3x3 Matrix, saved as a vector
string line;
ifstream myfile ("matrixelements.txt");
if(myfile.is_open())
{
while (getline (myfile.line))
{
cout<<line<<endl;
}
myfile.close();
}
else cerr<<"Unable to open file"<<endl;
}
The values are saved in the file matrixelements.txt like this:
#include<iostream>
#include<ostream>
#include<string>
#include<fstream>
usingnamespace std;
int main ()
{
double a[9]; // Space for a 3x3 Matrix, saved as a vector (technically it is an array)
string line;
ifstream myfile ("matrixelements.txt");
if(myfile.is_open())
{
int i = 0;
while ( i < 9 && myfile >> a[i] )
{
cout << a[i] << " ";
i++;
}
myfile.close();
}
else cerr<<"Unable to open file"<<endl;
}