开发者

Storing data in arrays

I have a data file that contains the following information for radius, density, acceleration, and pressure:

1.464e+22 1.403e-25 1.290e-08 6.325-10

1.539e+22 1.394e-25 1.680e-08 6.309-10

1.616e+22 1.384e-25 2.030e-08 6.289e-10

1.693e+22 1.373e-25 2.344e-08 6.266e-10

1.769e+22 1.362e-25 2.628e-08 6.239e-10

How can I store this data in arrays (e.g., radius[], density[])? The code to read the data file is as follows:

#include"stdio.h"

#define NDATA 5

int main()    
{    
    FILE *fo;

    int i;
    float r[NDATA];
    float rho[NDATA];
    float g[NDATA];
    float p[NDATA];

    fo = fopen("data.txt", "r");


    /* Read data */
    for (i = 0; i<NDATA; i++)
    {   
        fscanf(fo, "%f %f %f %f", &r[i], &rho[i], &g[i], &p[i]);
        printf(" read line %d %e %e %e %e\n", i, r[i], rho[i]开发者_Python百科, g[i], p[i]);
    }   
} 


#include"stdio.h"

#define NDATA 5

int main()    
{    
    FILE *fo;

    int i;
    float r[NDATA];
    float rho[NDATA];
    float g[NDATA];
    float p[NDATA];

    fo = fopen("data.txt", "r");
    fscanf();
    fscanf();

    /* Read data */
    for (i = 0; i<NDATA; i++)
    {   
        fscanf(fo, "%f %f %f %f", &r[i], &rho[i], &g[i], &p[i]);
        fscanf(fo);
        printf(" read line %d %e %e %e %e\n", i, r[i], rho[i], g[i], p[i]);
    }   
} 

This should work since you will not be reading in the first two lines and blank lines.


About the extern: I would try to avoid it. This would of been a comment, but comments are just to limited so a code block follows. It uses a very old C trick to malloc() a structure with variable sized members. This trick gives you a single pointer you can easily pass around to callers. The pointer points to a structure that contains everything in an easy to use format. Best of all it uses just a single malloc() to allocate all arrays, of whatever size is needed.

If you ever need to add new members, add them just before the "data" member and you will have fewer chances of bugs if everything is not recompiled as it should be.

data.h File

#include <stdlib.h>

typedef struct data_struct
{
    int ndata;
    float *r;
    float *rho;
    float *g;
    float *p;
    float  data[1];
} data_t;

static struct data_struct *data_malloc( int n )
{
    data_t *ptr = malloc( 
                sizeof(data_t) +           /* base struct size */
                n * sizeof(float) -        /* actual data */
                sizeof(float) );           /* remove extra data[0] */

    ptr->ndata = n;             /* populate size so users have it */

        /* while extending data_struct this way is horrid C++,
         * it is well used, classic, C, accepted by every compiler */
    ptr->r   = ptr->data;       /* beginning of data array */
    ptr->rho = ptr->r   + n;    /* follows r   */
    ptr->g   = ptr->rho + n;    /* follows rho */
    ptr->p   = ptr->g   + n;    /* follows g   */

    return   ptr;
}

static void data_free( struct data_struct *ptr )
{

    if ( ptr && ptr->ndata )
    {   /* have data, will free */
        ptr->ndata = 0;         /* zero the size as fail-safe */
        free( ptr );
    }

    return;
}

Main Program

#include 

#define NDATA 5

int main()
{
    FILE *fo;

    int i;
    data_t  *ptr = data_malloc( NDATA );

    fo = fopen("data.txt", "r");

    /* Read data */
    for (i = 0; i < NDATA; i++)
    {
        fscanf(fo, "%f %f %f %f", ptr->r+i, ptr->rho+i, ptr->g+i, ptr->p+i);
        printf(" read line %d %e %e %e %e\n", i,
                 ptr->r[i], ptr->rho[i], ptr->g[i], ptr->p[i]);
    }
    for (i = 0; i < NDATA; i++)
    {
        printf(" read line %d %e %e %e %e\n", i,
                 ptr->r[i], ptr->rho[i], ptr->g[i], ptr->p[i]);
    }

    some_function(ptr);  /* pass the pointer down as a single pointer */

    data_free(ptr);     /* always good to free anything you malloc */
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜