I am reading in from file into structure and came across problem. I have test file where first letter defines name of structure second number tells me how many nodes does it have and rest numbers are nodes. File example:
A 4 1 2 3 4
B 5 1 2 3 9 8
C 3 1 2 3
So for example structure should be this: name->A; numberOfNodes->4; nodes->{1,2,3,4}. My structure where I save each row is this:
struct mystruct{
char name[1];
int numberOfNodes;
int nodes[];
};
my function so far:
lines = lineCount(courses); //calculates how many rows file has
struct courses course[lines];
co = fopen(courses, mode);
if(co == NULL){
printf("Can't find the files.");
exit(1);
}else{
for(i = 0; i < lines; i++){
fscanf(co, "%1s %d \n", ¤t, &id1); //Doesnt have any problems reading these two parameters;
for(j = 0 ; j < id1; j++){
fscanf(co, "%d", &course[i].nodes[j]); //Have no idea how to store array =/
}
strcpy(course[i].courseName, current);
course[i].numberOfNodes = id1;
}
}
EDIT: I am sorry for confusing you guys, it allocates integers just fine but instead of outputting same thing it outputs something like this:
A 4 69 72 1 2
B 5 20 45 7 3 1
C 3 2 45 1
I think that this bit of code doesnt do what I want it to do:
for(j = 0 ; j < id1; j++){
fscanf(co, "%d", &course[i].nodes[j]); //Have no idea how to store array =/
}
Would appreciate any help!