my_funcs.c:
int x = 0;
FILE *stockIngredientes = fopen( "C:/Dropbox/Work/ISEC/P/ingredientes.txt", "r+" );
while( fgets( tmp, 100, stockIngredientes ) != NULL ){
ingredientes[x].nome = strtok( tmp, ":" );
ingredientes[x].identificadorNum = strtok( NULL, ":" );
ingredientes[x].quantStock = strtok( NULL, ":" );
ingredientes[x].limMin = strtok( NULL, ":" );
ingredientes[x].consumoMed = strtok( NULL, ":" );
x++;
}
printf("%s", ingredientes[0].nome);
printf("%s", ingredientes[4].nome);
Hey guys, my problem is as follows:
Looking at this piece of code im declaring a counter called x initialized as 0, followed by opening a file for read and write, I read all the lines of the file and in each line I use strtok to split the line into the data I need and assign it to my variables inside of a structure vector.
Now the problem comes, if I printf for example "ingredientes[x].nome" inside the while the values are all different, but outside the while all the elements of the vector become the same as the last element of the vector... I would like to know why this happens, I'm really confused.
printf("%s", ingredientes[0].nome);
printf("%s", ingredientes[4].nome);
Both these lines print the same name.. when clearly they shouldn't.