i have a structure:
Code:
typedef struct intrare //variabilele din in1.txt
{
char *nume;
double val;
} intrare;
and i am trying to read the following type of file
Code:
a=42 b=432 test=-423.2154
the function read is:
Code:
intrare *citire_var(FILE *f,int *n)
{
intrare *in=calloc(1,sizeof(intrare));
void *t;
char c[1];
int j=0;
*n=0;
while((c[0]=fgetc(f))!='\n')
{
if((c[0]!=' ')&&(c[0]!='='))
{
j++;
in[*n].nume=realloc(in[*n].nume,j*sizeof(char));
strncat(in[*n].nume,c,1);
}
if(c[0]=='=') fscanf(f,"%lf",&in[*n].val);
if(c[0]==' ')
{
j=0;
(*n)++;
printf("%d",*n);
t=realloc(in,(*n+1)*sizeof(intrare));
printf("<-\n");
if(t!=NULL) in=t;
else printf("Error\n");
}
}
(*n)++;
return in;
}
the problem is that after some steps it segm faults me.
please help!
thank you!