HI there
I usually program in Fortran but for a certain project i need to use some existing C routines.
I am mildly familiar with C but clearly not enough to get round this problem.
Below you will see a funciton called 'readinfiles' and then the main program below that.
Previously i had the content of readinfiles in the main program, but i want to be able to read in all my 3 files in a separate function, allocate all the arrays, pass them out of the function 'readinfiles' and into the main program and into the other functions shown below i.e rr_counts(xx_r,yy_r,zz_r,nrows2,nrows3,rad); dr_counts(xx_d,yy_d,zz_d,xx_r,yy_r,zz_r,nrows1,nro ws3,rad);
dd_counts(xx_d,yy_d,zz_d,nrows1,nrows3,rad);
so the arrays that are being allocated are xx_r,yy_r,zz_r,xx_d,yy_d,zz_d and rad . nrows1 and nrows3 are integers which are the number of rows in the arrays.
I have tried various ways to get the function to work - none of them successful.. mostly i get an error saying that e.g xx_r is not declared in int main (or something like that). The version below has been stripped back to very basic declaration at the top and return at the bottom.
Any help would be much appreciated!
--------------------------
Code:int readinfiles(){ int headercount1,linecount1,nrows1,headercount2,linecount2,nrows2; int headercount3,linecount3,nrows3; int i; FILE *file1,*file2,*file3; file1 = open_file_read("file1"); if(file1 == NULL) { exit(1); } get_file_length(file1,&headercount1,&linecount1); nrows1 = linecount1 - headercount1; printf("there are %d in FIRST data\n",nrows1); (void) rewind(file1); long double *xx_d = malloc(sizeof(long double) * nrows1); long double *yy_d = malloc(sizeof(long double) * nrows1); long double *zz_d = malloc(sizeof(long double) * nrows1); for(i=0; i<nrows1; i++) { fscanf(file1,"%Le %Le %Le",&xx_d[i],&yy_d[i],&zz_d[i]); } fclose(file1); file2 = open_file_read("file2.dat"); if(file2 == NULL) { exit(1); } get_file_length(file2,&headercount2,&linecount2); nrows2 = linecount2 - headercount2; printf("there are %d in random data\n",nrows2); (void) rewind(file2); long double *xx_r = malloc(sizeof(long double) * nrows2); long double *yy_r = malloc(sizeof(long double) * nrows2); long double *zz_r = malloc(sizeof(long double) * nrows2); for(i=0; i<nrows2; i++) { fscanf(file2,"%Le %Le %Le",&xx_r[i],&yy_r[i],&zz_r[i]); } fclose(file2); file3 = open_file_read("file3.dat"); if(file3 == NULL) { exit(1); } get_file_length(file3,&headercount3,&linecount3); nrows3 = linecount3 - headercount3; printf("there are %d in rad.dat\n",nrows3); long double *rad=malloc(sizeof(long double) *nrows3); for(i=0; i<nrows3; i++) { fscanf(file3,"%Le",&rad[i]); printf("%Le\n",rad[i]); } fclose(file3); return ; } /*----------------------------------------------------------------------------*/ /*---------------M A I N P R O G R A M ------------------------------------*/ int main(int argc, char **argv) { readinfiles(); rr_counts(xx_r,yy_r,zz_r,nrows2,nrows3,rad); dr_counts(xx_d,yy_d,zz_d,xx_r,yy_r,zz_r,nrows1,nrows3,rad); dd_counts(xx_d,yy_d,zz_d,nrows1,nrows3,rad); twoptcorr(); return 0; }



LinkBack URL
About LinkBacks



