2016-12-10 2 views
0

Donc maintenant je suis simplement en train de prendre un fichier de données factices,avec gsl_matrix_fscanf (fichier, matrice)

1.30640 1 
0.91751 1 
0.49312 1 
0.49312 0 
1.79859 1 
1.86360 1 
0.12313 1 
0.12313 0 
-0.19091 1 
1.82377 1 
0.63205 1 
0.63205 0 
1.23357 1 
0.62110 1 
0.80438 1 

et au magasin moment comme un gal_matrix pour les manipulations ultérieures. Ci-dessous se trouve un code qui, pour le moment, compte tenu du nom d'un fichier de données, détermine la longueur de celui-ci (nombre de lignes), initialise une structure gsl_matrix et essaie de scanner le fichier texte dans cette matrice appelée chaîne.

#include <stdio.h>   // Needed for printf() and feof() 
#include <math.h>   // Needed for pow(). 
#include <stdlib.h>   // Needed for exit() and atof() 
#include <string.h>   // Needed for strcmp() 
#include <gsl/gsl_matrix.h> // Needed for matrix manipulations 

/*------------ Defines -----------------------------------------------------------------*/ 
#define MAX_SIZE 10000000 // Maximum size of the time series array 
#define NUM_LAG 1000  // Number of lags to calculate for 


/* 
*---------------------------------------------------------------------------------------- 
* Main program 
*---------------------------------------------------------------------------------------- 
*/ 
int main(int argc, char* argv[]) { 

//--------Initialization-------------------------------------------------------------- 
    double ac_value;  // computed autocorrelation value 
    int i,j;    // Loop counter 
    long int N; 
    double mean, variance; 
    gsl_matrix * chains; 

    char filename[100]; 
    FILE* in_file;  // input file 
    FILE* out_file;  // output file 

    int no_params;  // number of parameters to calculate autocorrelation for 
    int first_column; // Which column first corresponds to a chain 
    int ch;    // to determine number of samples in file 

    printf("-------------------------------------------------------------------------\n"); 
    //--------Check that there are the correct number of arguments passed----------------- 
    if(argc != 4) { 
     printf("usage: ./auto_corr chainfile no_params first_column \n"); 
     exit(1); // 0 means success typically, non-zero indicates an error 
} 

    //--------Extract arguments----------------------------------------------------------- 
    sprintf(filename,"%s",argv[1]); // convert input file to string 
    in_file = fopen(filename,"rb"); // open input file for reading 

    no_params = atoi(argv[2]);  
    first_column = atoi(argv[3]); 

    //--------What is the number of samples in chain file?-------------------------------- 
    N = 0; // Initialize count 
    while(!feof(in_file)) { 
     ch = fgetc(in_file); 
     if(ch == '\n'){ 
      N++; 
     } 
    } 
    printf("Number of samples: %li\n", N); // print number of samples 
    if (N > MAX_SIZE) { // throw error if there are too many samples 
     printf("ERROR - Too many samples! MAX_SIZE = %i", MAX_SIZE); 
     exit(2); 
    } 

    //--------Generate a gsl matrix from the chains--------------------------------------- 
    printf("%i\n", no_params); 
    chains = gsl_matrix_alloc(N, no_params); // allocate memory for gsl_matrix(rows, cols) 
    // print the matrix (for testing) 
    printf("Chain matrix \n"); 
    for (int m=0;m<N;m++) { //rows 
     for (int n=0; n<no_params;n++) { // columns 
      printf("%f ",gsl_matrix_get(chains,m,n)); 
     } 
     printf("\n"); 
    } 
    // gsl_matrix_fprintf(stdout,chains,"%f"); // easy way to print, no formatting though 
    gsl_matrix_fscanf(in_file, chains);  // read in chains to the gsl_matrix 
    fclose(in_file); 

L'erreur se produit dans la ligne de gsl_matrix_fscanf, et la sortie que je vois est

$ ./auto_corr auto_corr_test_data.dat 2 0 
------------------------------------------------------------------------- 
Number of samples: 15 
2 
Chain matrix 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
0.000000 0.000000 
gsl: ./fprintf_source.c:165: ERROR: fscanf failed 
Default GSL error handler invoked. 
Abort trap: 6 

Répondre

0

J'ai oublié de revenir en arrière le fichier d'entrée après avoir déterminé le nombre de lignes.