Link to home
Start Free TrialLog in
Avatar of samj
samj

asked on

reading data file into a gsl matrix

Hi

both gsl_matrix_fread and gsl_matrix_fscanf require that matrix  must
be preallocated with the correct dimensions. now if I want to load the
data from a file and I don't know the number of rows and columns. do I
write a code to getline(in, line) and count++ or there is a better
way.
I know how to count the number of line from ifstream, but don't know howto get the number of words in one line which is the number of the column in the file, so that I can feed these info to the gsl_matrix for the size of the matrix to be allocated.

   ifstream in(file_name.c_str());
   string line = getline(in, line);
   stringstream input( line.c_str() ); <<-- error here
   
   string word;
   nCol = 0;
   while(inpput >> word)
      nCol++;

   nRows = 1;
   while (getline(in, line))
      nRows++;


thanks


Avatar of rajeev_devin
rajeev_devin

>> string line = getline(in, line);
This is a wrong syntax. It would be

string line;
getline(in, line);
ASKER CERTIFIED SOLUTION
Avatar of rajeev_devin
rajeev_devin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> ifstream in("data.txt");
Change this to your previous statement.

ifstream in(file_name.c_str());