I've got the following example :
#define MAX_SIZE 32
typedef struct T {
int total_data;
D *data;
} T;
typedef struct D {
int type;
char value[MAX_SIZE];
} D;
I've got a part which extract and fill D* data; And one who print it.
The part which extract and fill D extract data from a file.
void extract(T *_t) {
// Open file
fscanf(fp, "%d\n", &_t->total_data);
_t->data = malloc(_t->total_data * sizeof(*_t->data);
// Extract and fill
for ( i = 0; i < _t->total_data; i++)
fscanf(fp, "%d:%[^$]\n", &(_t->data[i].type), _t->data[i].value);
}
The function which read looks like this :
void read(T *_t) {
int i;
for( i = 0; i < _t->total_data; i++)
printf("%d - %s", _t->data[i].type, _t->data[i].value);
}
But I've got a crash .. I don't know why .. the code looks ok for me according to what I've found on the web. Could you please help me ?
Thanks.
Files look like that :
2
0:ABC
1:DEFGHI
For exemple
NULL. Especially given the fact you're reading from a file, you could run out of memory. Always assume you might run out of memory anyway :)