Solved

reading data file into a gsl matrix

Posted on 2006-07-20
3
619 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
>> ifstream in("data.txt");
Change this to your previous statement.

ifstream in(file_name.c_str());
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

763 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

12 Experts available now in Live!

Get 1:1 Help Now