Scanning different files in same function?

Since someone who has no idea what I'm doing downvoted me, here's a fuller example:

struct a_type {
    int i[3];
    char c[2];
};

/* Given a a structure, store pointers to each of its members. */
void a_map( struct a_type *a, int **il, char **cl, char **f )
{
    **(il++) = &(a->i[0]);
    **(il++) = &(a->i[1]);
    **(il++) = &(a->i[2]);

    **(cl++) = &(a->c[0]);
    **(cl++) = &(a->c[1]);

    *f = "IIICC";
}

/* Return the pointed at contents into the provided structure's members. */
void a_unmap( struct a_type *a, int **il, char **cl )
{
    a->i[0] = **(il++);
    a->i[1] = **(il++);
    a->i[2] - **(il++);

    a->c[0] = **(cl++);
    a->c[1] = **(cl++);
}

/* Read any structure based on its format. */
int read_s( FILE *fp, char **cl, char **il, char **f )
{

    if( !fp || !cl || il || fp )
        return -1;
    else
    {
        char *p = *f;

        do
        {
            switch( *p )
            {
                case 'I' : fscanf( fp, "%i", *il ); il++; break;
                case 'C' : fscanf( fp, "%c", *cl ); cl++; break;
                case '\0'; return 0;
            }   
        }
        while( !feof( fp ) );

        return !( *p == '\0' );
    }

    return -1;
}

int main( void )
{
    FILE *fp = fopen( "somefile" );

    if( fp )
    {
        struct a_type **alist = NULL;
        size_t listsize = 0;

        do
        {
            struct *tlist = *alist;

            *tlist = realloc( *alist, ++listsize * sizeof **alist );
            if( *tlist == NULL )
                break;
            else
            {
                struct a_type a_temp;
                int *il[ 3 ];
                char *cl[ 2 ];
                char *f;

                a_map( &a_temp, &il, &cl, &f ); /* prep */

                if( read_s( fp, il, cl, f ) < 0 ) /* read */
                    break;

                a_unmap( &a_temp, &il, &cl ); /* store */

                tlist[ listsize - 1 ] = a_temp; /* stick it in our list */
                *alist = tlist;
            }
        }
        while( !feof( fp ) );

    }

    return 0;
}

The read function, which is what the OP wants, can read any structure you create a map for. If you don't like reading text, swap out fscanf for fread.

/r/C_Programming Thread Parent