c - read from csv, strtod can't read string with numbers -


when read follow file

"x","pa(x)","time","pa","xixj","value" "x",0,5,0,"0-0","123.814935276" " ","  "," "," ","0-1","234" " ","  "," "," ","0-2","100" " ","  "," "," ","1-0","166" " ","  "," "," ","1-1","203.0866414" " ","  "," "," ","1-2","383" " ","  "," "," ","2-0","186" " ","  "," "," ","2-1","338" " ","  "," "," ","2-2","173.0984233" " ","  ",10,0,"0-0","186.221113" "  ","  ","  ","  ","0-1","391" "  ","  ","  ","  ","0-2","64" "  ","  ","  ","  ","1-0","235" "  ","  ","  ","  ","1-1","195.7454998" "  ","  ","  ","  ","1-2","275" "  ","  ","  ","  ","2-0","218" "  ","  ","  ","  ","2-1","121" "  ","  ","  ","  ","2-2","118.0333872" " ","  ",20,0,"0-0","416.36349977" " ","  "," "," ","0-1","282" " ","  "," "," ","0-2","735" " ","  "," "," ","1-0","278" " ","  "," "," ","1-1","211.8960279" " ","  "," "," ","1-2","266" " ","  "," "," ","2-0","743" " ","  "," "," ","2-1","224" " ","  "," "," ","2-2","371.7404745" 

my code fails read value column. use strtod results are:

x "x" pax "pa(x)" time "time"  pa "pa"  xixj "xixj"  value 0.000000  start csv  x "x" pax 0 time 5  pa 0  xixj "0-0"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "0-1"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "0-2"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "1-0"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "1-1"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "1-2"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "2-0"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "2-1"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "2-2"  value 0.000000  start csv  x " " pax "  " time 10  pa 0  xixj "0-0"  value 0.000000  start csv  x "  " pax "  " time "  "  pa "  "  xixj "0-1"  value 0.000000  start csv  x "  " pax "  " time "  "  pa "  "  xixj "0-2"  value 0.000000  start csv  x "  " pax "  " time "  "  pa "  "  xixj "1-0"  value 0.000000  start csv  x "  " pax "  " time "  "  pa "  "  xixj "1-1"  value 0.000000  start csv  x "  " pax "  " time "  "  pa "  "  xixj "1-2"  value 0.000000  start csv  x "  " pax "  " time "  "  pa "  "  xixj "2-0"  value 0.000000  start csv  x "  " pax "  " time "  "  pa "  "  xixj "2-1"  value 0.000000  start csv  x "  " pax "  " time "  "  pa "  "  xixj "2-2"  value 0.000000  start csv  x " " pax "  " time 20  pa 0  xixj "0-0"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "0-1"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "0-2"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "1-0"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "1-1"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "1-2"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "2-0"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "2-1"  value 0.000000  start csv  x " " pax "  " time " "  pa " "  xixj "2-2"  value 0.000000 

and value 0. how can fix problem? here code :

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/time.h> #include <math.h> #include <stdbool.h> #include <ctype.h>  #define bufsiz 1024  #define arraysize(x)  (sizeof(x)/sizeof(*(x)))  double calculatemlpa(const char *xn_val[], char *traj[], float value[], double alphaxixj, double tauxi, int sz, int dim) {     double mlx = 0;     double v;     double alphaxi;     char *state;     int i;     int p;     int j;     int k;     //  int sz = sizeof(xn_val) / sizeof(int);     //  int dim = sizeof(traj) / sizeof(char);     double trns[sz][sz];     double m[sz];     char *trat="-";        // m[xi] values: number of transitions leaving state xi     printf("%d %d \n",sz,dim);     int cont=0;     (i = 0; < sz; i++) {         m[i] = 0.0;         (j = 0; j < sz; j++) {             v = 0.0;             int newlength = strlen(xn_val[i])+strlen(trat)+strlen(xn_val[j])+1;             state = malloc(sizeof(char)*newlength);             if(state != null){                 state[0] = '\0';                 strcat(state,xn_val[i]);                 strcat(state,trat);                 strcat(state,xn_val[j]);                 printf("%s ",state);             }else {                 printf(stderr,"malloc failed!\n");             }             //          (k=0; k<=dim;++k){             if (traj[cont] != null ){                 if (strcmp(traj[cont],state)==0){                     v = value[cont+1];                     printf("%f \n",v);                 }             }              trns[i][j] = v;              printf("%f - \n",trns[i][j]);             if (strcmp(xn_val[i],xn_val[j])!=0)                 m[i] = m[i] + v;              cont++;         }     }     (i=0;i<sz;++i){         for(j=0;j<sz;++j){              printf("%f ",trns[i][j]);         }         printf("\n");     }       (p=0;p<sz;++p){         printf("%f - \n",m[p]);     }     alphaxi = alphaxixj * (((double) sz) - 1.0);     alphaxi = alphaxixj;     //printf("%d ",sz);     (i = 0; < sz; i++) {         (j = 0; j < sz; j++) {             // xi!=xj             if (strcmp(xn_val[i], xn_val[j])!=0) {                 mlx = mlx + lgamma(alphaxixj + trns[i][j]) - lgamma(alphaxixj);             }             // xi             else {                 mlx = mlx + lgamma(alphaxi) - lgamma(alphaxi + m[i]);                 mlx = mlx + lgamma(alphaxi + m[i] + 1.0)+ (alphaxi + 1.0) * log(tauxi);                 mlx = mlx - lgamma(alphaxi + 1.0)- (alphaxi + m[i] + 1.0) * log(tauxi + trns[i][j]);             }         }     }      return (mlx); }  void main() {     printf("inizio\n");     file *pf;     int n=20;     int f,kk=0,k=0,i;     bool first=true;     const char *a[]={"0","1","2"};     char *traject[]={"0-0","0-1","0-2","1-0","1-1","1-2","2-0","2-1","2-2"};     double bs=0;     char *str;     char *trat="-";     int file;      //for (file=0;file<4;++file){     pf=fopen("//home//user//prova0.csv","r");      //float array[10][10];     float *t;      //char *state[];     t = (float *)malloc(n * sizeof(float));      float val;     if (pf)     {          char buffer[bufsiz], *ptr;         /*          * read each line file.          */         while(fgets(buffer, sizeof buffer, pf)){             k++;         }         fclose(pf);         pf=fopen("//home//user//prova0.csv","r");         printf("k=%d\n",k);         char *state[k];         while(fgets(buffer, sizeof buffer, pf)) {             //k=0;             printf("start csv \n");             char *x;             char *pax;             int time;             char *pa;             char *xixj;             double value[k];             char *token;             char *ptr = buffer;             const char end[2]=",";//fgets(buffer, sizeof buffer, pf);             token = strtok(ptr, end);             f=0;             /* walk through other tokens */             while( token != null )             {                  if(f==0){                     x=token;                     printf( "x %s\n", token );                 }else if(f==1){                     pax=token;                     printf( "pax %s\n", token );                 }                 else if(f==2){                     time=(token);                     printf( "time %s \n", token );                  }                 else if(f==3){                     pa=token;                     printf( "pa %s \n", token );                  }                 else if(f==4){                     xixj=(token);                     printf( "xixj %s \n", token );                  }                 else{                     char *str;                     value[kk]=strtod(token, &str);                     printf("value %f \n", value[kk]);                     kk++;                  }                 token = strtok(null, end);                 f++;              }          }           fclose(pf);     }      else /* fopen() returned null */     {         perror(pf);     }         }      printf("\nstart\n");     int sz=arraysize(a);     int dim=arraysize(traject);     bs=calculatemlpa(a,traject,t,1.0,0.1,sz,dim);     printf("done file%d \n",file);     printf("%f ",bs);     //}  } 

you read values ", need rid of them before parsing double, understand.

xixj "0-1" , printf( "xixj %s \n", token ); means token = <"0-1">, not <0-1>, same value, <"383"> , not <383>.

so should rather used:

value[kk]=strtod(&token[1], null); 

this:

printf("value %d \n", value[kk]); 

should be

 printf("value %f \n", value[kk]); 

since value[k] double.

and different - should consider working coding standards, things one-letter names make code less maintainable , readable.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -