reading data file into a gsl matrix

Posted on 2006-07-20
Last Modified: 2008-03-10

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
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)

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


Question by:samj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 12

Expert Comment

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

string line;
getline(in, line);
LVL 12

Accepted Solution

rajeev_devin earned 125 total points
ID: 17152273
Your modified code will be

ifstream in("data.txt");
string line;
getline(in, line);
stringstream input( line.c_str() );
string word;
int nCol = 0;
while(input >> word)

int nRows = 1;
while (getline(in, line))
LVL 12

Expert Comment

ID: 17152298
>> ifstream in("data.txt");
Change this to your previous statement.

ifstream in(file_name.c_str());

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question