#include #include typedef struct student{ char name [20]; int id; }student; typedef struct node{ student data; struct node *next; }node; void Push(node** headRef, student data){ node * newNode = (node *) malloc(sizeof(node)); if (!newNode) return; newNode->data = data; newNode->next = *headRef; *headRef = newNode; } node * readStds(char * filename){ //first - we have to return the linked list of students, so we have to allocate it // inside the heap so we avoid TAB-Victim problem //second- we do not really know the size of the elements to be read, // so read and push one element at a time //third- we have to maintain the order of the students, // so we push at the end node * head = NULL, **tailRef = &head; unsigned long ans, r = 0; student s; // I need one local variable //open the file FILE *f; if(!(f = fopen(filename, "rb"))) return NULL; while(1){ if (fread(&s, sizeof s, 1, f) != 1) break; Push(tailRef, s); tailRef = &((*tailRef)->next); } //close the file ans = fclose(f); return head; } void printStudent(student s){ printf("student [name = %s, id = %d ]\n", s.name, s.id); } void test(){ char *fname = "students"; int array_size = 0, i; node * head = readStds(fname), *current; for(current = head ; current ; current = current->next){ printStudent(current->data); } } int main(void){ test(); return 0; }