2016-12-10 11 views
0

So jetzt bin ich einfach versucht, eine Dummy-Datendatei zu nehmen,Fehler mit gsl_matrix_fscanf (Datei, Matrix)

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 

und zur Zeit speichert es als gal_matrix für Manipulationen später. Unten ist ein Code, der einfach im Moment, wenn ein Datendateiname herausgefunden wird, wie lange er ist (d. H. Anzahl von Zeilen), eine gsl_matrix-Struktur initialisiert und dann versucht, die Textdatei in diese Matrix zu scannen, die als Kette bezeichnet wird.

#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); 

der Fehler auftritt in der gsl_matrix_fscanf Leitung, und der Ausgang I ist zu sehen, am

$ ./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 

Antwort

0

I vergessen haben, die Eingabedatei zurückzuspulen, nachdem ich die Anzahl der Zeilen bestimmt.

Verwandte Themen