?
Solved

reading data file into a gsl matrix

Posted on 2006-07-20
3
Medium Priority
?
696 Views
Last Modified: 2008-03-10
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


0
Comment
Question by:samj
[X]
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
3 Comments
 
LVL 12

Expert Comment

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

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

Accepted Solution

by:
rajeev_devin earned 375 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)
      nCol++;

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

Expert Comment

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

ifstream in(file_name.c_str());
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 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