Question
Please do I need to use fscanf again as I did below or not?
For an example I have 5 patients in my text file below:
Pete Okay 45 345
Maud Okay 36 536
Caleb Dimitri 15 123
Cadrin Christopher 9 695
And below is the structure definition:
struct patient
{
char firstname[20];
char lastname[20];
int age;
int id;
struct patient *link;
};
struct clinic
{
char nameofclinic[30];
struct patient *linklist;
};
void patients(FILE *xfiles, struct patient *mems);
main()
{
FILE*file;
struct clinicmembers;
struct patient *xpter
file openstatements..........
patients(file, xpter);
fclose(....);
}
void patients(FILE *xfiles, struct patient *mems)
{
members->linklist =(struct patient *) malloc(1 * sizeof(struct patient);
fscanf(xfiles, "%s %s %d %d",mems->firstname, mems->lastname,&(mems->age).........);
and I assigned the first node to thefirst pointer like below
members->linklist =mems;
I MIGHT ADD SOME MORE FUNCTIONSBUT PLEASE IF I ADD A NODE TO AN EMPTY LIST LIKE BELOW ANDADDING A NODE AS THE NEW HEAD OF THE LIST. DO I NEED TOALLOCATE MEMORY FOR EACH NODE OR ALL I NEED IS ONE NODE WHICH IALREADY DONE ABOVE?
members->linklist->link = NULL;
}
THIS IS MY FIRST TIME USING LINK LIST SO I'M NOT SURE WHAT INEED TO DO NEXT, DO YOU THINK FSCANF WILL BE NEEDED AGAIN OR THAT'SIT, AND PLEASE WHAT WILL THE NEXT STATEMENT WILL BE AT ABOVE?
Explanation / Answer
You have to malloc for every node you add. The point of a linked list is that the memory is not allocated inadvance (as in arrays) and you allocate the memory only when youneed it, the data being fragmented over the memory space linkedwith your pointers from node to node. based on your code, added the initialization code: #include #include #include struct patient { char firstname[20]; char lastname[20]; int age; int id; struct patient *link; }; struct clinic { char nameofclinic[30]; struct patient*patients_list; }; void add_patients_from_file(FILE *i_file, struct clinic*i_clinic_p); void print_clinic(struct clinic *i_clinic_p); void print_patient(struct patient *i_patient_p); int main() { FILE *file; struct clinic my_clinic; my_clinic.patients_list = NULL; if( (file = fopen( "patients.txt", "r" )) !=NULL ) { add_patients_from_file(file, &my_clinic); fclose( file ); } else { printf("couldn't findfile "); } print_clinic(&my_clinic); return 0; } void add_patients_from_file(FILE *i_file, struct clinic*i_clinic_p) { struct patient *cur_patient; struct patient *next_patient; next_patient = (struct patient *) malloc(1 *sizeof(struct patient)); next_patient->link = NULL; while (fscanf(i_file, "%s %s %d %d",next_patient->firstname, next_patient->lastname,&(next_patient->age), &(next_patient->id)) != EOF){ if(!i_clinic_p->patients_list) { // first patient i_clinic_p->patients_list = next_patient; } else { cur_patient->link = next_patient; } cur_patient =next_patient; next_patient = (structpatient *) malloc(1 * sizeof(struct patient)); next_patient->link =NULL; } // while free(next_patient); } void print_clinic(struct clinic *i_clinic_p) { if (!i_clinic_p->patients_list) { printf("nopatients "); return; } struct patient *cur_patient; cur_patient = i_clinic_p->patients_list; while (cur_patient) { print_patient(cur_patient); cur_patient =cur_patient->link; } } void print_patient(struct patient *i_patient_p) { printf("%s %s %d %d ",i_patient_p->firstname, i_patient_p->lastname,i_patient_p->age, i_patient_p->id); }