c - Scanning data into a multidimensional array -


i given data file included mass , (x,y,z) position of point. want take data given , output file using multidimensional array. don't know how take scanned data , use in array, however.

i have data stored follows:

for(i = 0; < n; i++)      //scan in file data {     fscanf(fpin, "%d %d %d %d", &array_x[i], &array_y[i], &array_z[i], &array_mass[i]); } 

i tried setting array [][4] since number of rows dependent on number of data points given (and instructions write array work number of data points, not given) , there 4 columns (one x, y, z, , mass respectively).

then tried setting as:

for(i = 0; < n; i++) {     array_out[i][4] = { {array_x[i]}, {array_y[i]}, {array_z[i]}, {array_mass[i]} }; } 

and printing as:

for(i = 0; < n; i++)     for(j = 0; j < n; j++)         fprintf(fpout, "%d %d %d %d", array[i][j]); 

but doesn't work.

do know how many elements need read? if do, can allocate array hold them. if not need dynamic storage.

what types of mass , point(x,y,z)? please provide samples.

you may have issues printf format specifier (do mean %d, not double)? (see: c printf using %d , %f)

declare struct contain point/mass, , essential functions (methods) deal pointmass(es),

declare storage,

typedef struct pointmass_struct {     double mass;     double x,y,z; } pointmass_t ; 

declare functions/methods,

pointmass_t* pm_new( double mass, double x, double y, double z ) {     pointmass_t* this;     if( ! (this=malloc(sizeof(pointmass_t)) ) return(null);     this->mass = mass;     this->x = x;     this->y = y;     this->z = z;     return(this); } void pointmass_del( pointmass_t* pm ) {     if(pm) free(pm);     return; } pointmass_t* pointmass_fromstr( char* line ) {     double x, y, z, m;     sscanf(line, "%f %f %f %f", &x, &y, &z, &m );     pointmass_t* = pointmass_new( x, y, z, m );     return( ); } pointmass_t* pointmass_fscanf( file* fp ) {     double x, y, z, m;     fscanf(fp, "%f %f %f %f", &x, &y, &z, &m );     pointmass_t* = pointmass_new( x, y, z, m );     return( ); } char* pointmass_tostr( pointmass_t* this, char* buff ) {     if( !buff ) return(buff);     sprintf(buff, "%f %f %f %f", this->x, this->y, this->z, this->mass );     return(buff); } int pointmass_print( pointmass_t* ) {     char pmbuff[100];     return( printf("%s\n",pointmass_tostr(this,pmbuff) ) ); } 

scan 1 pointmass,

pointmass_t* pm = pointmass_fscanf(fpin); 

scan many pointmass(es) declared array of pointmass,

pointmass_t* pm_array[100]; //pick suitable size for(i = 0; < n; i++) //scan point/mass elements file {     pm_array[i] = pointmass_fscanf(fpin); }  char pmbuff[100]; for(i = 0; < n; i++) //print them {     printf("[%d] %s\n",pointmass_str(pm_array[i],pmbuff)); } 

track how large array is, , dynamically increase size,

pointmass_t** pm_dynarray; int pmsize = 0; pm_dynarray = malloc( (pmsize=500) * sizeof(pointmass_t*)); for(i = 0; < n; i++) //scan file {     if( i+1 > pmsize ) {         pm_dynarray = realloc( (pmsize+=100) * sizeof(pointmass_t*));     }     pm_dynarray[i] = pointmass_fscanf(fpin); }  for(i = 0; i<pmsize; i++ ) {     printf("[%d] %s\n",i,pointmass_str(pm_dynarray[i],pmbuff)); } 

remember free of these pointmass elements,

for(i = 0; i<pmsize; i++ ) {     if(pm_dynarray[i]) pointmass_del(pm_dynarray[i]);     pm_dynarray[i] = null; } //now safe free array free(pm_dynarray); pm_dynarray=null; pmsize=0; 

better yet, use linked list store pointmass elements.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -