i have some issue with my code i read a file and i push the data inside the file into a struct data type
but when iterate over the dataValue which should be an array of struct it give me the same dataValue and specificly the last one in the file.
int main() {
FILE *file;
struct characteristic {
double * dval;
double n;
char * name;
};
struct characteristic dataValue[150];
if ((file = fopen("/XXX/XXX/XXX/XXX/data", "r"))
== NULL) {
puts("error reading file");
exit(1);
} else {
int i = 0;
double var1, var2, var3, var4;
char * etiq = malloc(sizeof(char) * 100);
while (fscanf(file, "%lf,%lf,%lf,%lf,%s \n", &var1, &var2, &var3, &var4,
etiq) != EOF) {
struct characteristic *new_node;
new_node = (struct characteristic *) malloc(
sizeof(struct characteristic));
new_node->dval = &var1;
(new_node->dval)++;
new_node->dval = &var2;
(new_node->dval)++;
new_node->dval = &var3;
(new_node->dval)++;
new_node->dval = &var4;
new_node->name = etiq;
puts("=================================================");
printf("the name of characteristic number %d est : %s .\n", i,
new_node->name);
dataValue[i].dval = new_node->dval;
dataValue[i].name = new_node->name;
dataValue[i].n = i;
i++;
}
}
int i,j;
for (i = 0; i < sizeof(dataValue) / sizeof(struct characteristic); i++) {
printf("second name of characteristic number %d est : %s .\n", i,
dataValue[i].name);
for (j = 0; j < 4; j++) {
printf("bla value of vector number %d at index %d est : %lf \n", i,
j, *(dataValue[i].dval + j));
}
}
return EXIT_SUCCESS;
}
the file containe data like : 1.0,2.7,4.9,1.5,name
(new_node->dval)++is incrementing the pointer, Afterwards you are assigning it with an address&var4in the end. What is the point of all of this exercise?