Hello, I just starting with C and I am having a problem that I can´t understand. Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
FILE *M;
M=fopen( "m", "r");
char line [ 540 ];
long int i=0;
struct estr_reg {
char *molid;
char *molc;
};
struct estr_reg reg[6];
while ( fgets ( line, sizeof line, M ) != NULL )
{
reg.molid=strtok(line,";");
reg.molc=strtok(NULL,";");
i++;
};
fclose ( M );
printf("%s %s", reg[0].molid,reg[1].molid);
return 0;
}
the problem is that the output of this printf is always "5 5" that is the value of reg[6].molid (the last element of reg). If I do the printf inside the "while (fgets...", the value displayed is correct. But outside the while, it is as if all the elements of the array collapse into one (the last one). Why is this???????
Thanks!!!