Solved

reading data file into a gsl matrix

Posted on 2006-07-20
3
641 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
  • 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 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)
      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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now