G'ah my c programming needs major improvement. I've created a function that reads information from files and then enters this information in a linked list.
What I've done is a bit of a mess as I'm not at all confident what I'm doing is correct, in fact I know its wrong. I'll give you the code I've written and then write down what i'm trying to achieve with it.
Code:
ListNode *read_vectorfiles(FileNames vectorFile)
{
FILE *fptr;
int vectorCount, i=0;
ListNode *head=(ListNode *)malloc(sizeof(ListNode));
ListNode *node=0;
Colour *colour=colour_create();
Vector *position=vector_create();
/*Open the file*/
fptr=fopen(vectorFile.fileName, "r");
fscanf(fptr, "Vector Count: %d\n\n", &vectorCount);
node=head;
for(i=0; i<vectorCount; i++){
fscanf(fptr, "position: %f %f %f %f", &position->x, &position->y, &position->z, &position->w);
fscanf(fptr, "colour: %f %f %f %f\n", &colour->r, &colour->g, &colour->b);
node->next=(ListNode *)malloc(sizeof(ListNode));
node->next->colour=(Colour *)malloc(sizeof(Colour));
node->next->vector=(Vector *)malloc(sizeof(Vector));
node->next=vectorlist_setnode(position, colour);
node=node->next;
}
printf("Here is one of the read colours apparently %.2f\n\n", head->next->colour->r);
return(head);
}
I'm allocating a whole load of memory - is the allocation correct? and I'm doing this as it reads the relevant details and then I'm sending the newly read data off to a function to set the values.
The reason i have a node=head in there is because I want to return the head and using the node to loop through the linked list allows me to do that, but I think what I've done there is wrong also.
The structure of my ListNode:
Code:
typedef struct tagListNode{
Vector *vector;
Colour *colour;
struct tagListNode *next;
}ListNode;
This is all a complete mess I know but any help is much appreciated.
Cheers