help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] ERROR: fscanf failed


From: Fred J.
Subject: [Help-gsl] ERROR: fscanf failed
Date: Sun, 23 Jul 2006 12:21:45 -0700 (PDT)

Hi
I am getting this error "ERROR: fscanf failed" upon running the c++ program 
below, the docs says "The function returns 0 for success and GSL_EFAILED if 
there was a problem reading from the file."
in this case it is returning neither, rather somthing totally different.

thanks for helping

**************** read_data.h ****************
#ifndef READ_DATA_H
#define READ_DATA_H
#include <string>
#include <cstdio>
#include <gsl/gsl_matrix.h>

class read_data
{
   int nRows, nCol;
   std::string file_name;
   void set_No_of_Rows_Cols();
   gsl_matrix * m;        // from gsl_matrix.h
   void matrix_the_file();
   
public:
   read_data(std::string const& fileName);
   ~read_data();
   
};
#endif



**************** read_data.cpp ****************
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <gsl/gsl_matrix.h>
#include "read_data.h"

using namespace std;

read_data::read_data( string const& fileName )
   : file_name( fileName ){
   nCol = 0;
   nRows = 1;
   set_No_of_Rows_Cols();
   matrix_the_file();
}
void read_data::set_No_of_Rows_Cols() {
   ifstream in(file_name.c_str());
   string line;
   getline(in, line);
   stringstream input( line.c_str() );
   
   string word;
   while(input >> word) 
      nCol++;            // init'd by constructor

   while (getline(in, line))
      nRows++;            // init'd by constructor
}
void read_data::matrix_the_file(){
   // allocate memory for the matrix
   // needs to be freed later using
   // void gsl_matrix_free (gsl_matrix * m)
   m = gsl_matrix_alloc (nRows, nCol);
   FILE * f = fopen(file_name.c_str(), "rb");
   gsl_matrix_fscanf (f, m);
   fclose(f);
   cout << "data read" << endl;
}
read_data::~read_data() {}

/*
std::string command = "wc -l";
std::system( ( command + " " + file_name ).c_str() );
}
*/



**************** read_data_test.cpp ****************
#include <fstream>
#include <iostream>
#include <string>
#include "read_data.h"

using namespace std;

int main() {
   string f = "../../data/ZB/Jun06/20060405";
    read_data data1( f );    // space delimited
   
}


                        
---------------------------------
See the all-new, redesigned Yahoo.com.  Check it out.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]