Take a look at
std::basic_istream<CharT,Traits>::operator>> - cppreference.com[
^]
Look at the example code, which shows you how to read a value from a file into a variable. This assumes that your data (
#1, #2, #3
etc) are all normally formatted numbers, separated by white space (space, tab, etc), otherwise you'll have to massage your data on read. There's also an assumption here that you know how many rows and columns you have, so can arrange to pre-size (either dynamically using
new
or a smart pointer, or statically with something like
double data[100][100]
If you don't know the rows and columns ahead of time, you might want to consider using a vector of vectors (e.g.
vector<vector<double>>
. In this case you'd use getline() to read in a line of text from the file, parse out the columns (see
std::basic_stringstream - cppreference.com[
^] on how you might do that) to construct the row vector, and then
push_back()
the row vector into the enclosing matrix.