This is c language and I have a file which I want to read it tothis array of poi
ID: 3612777 • Letter: T
Question
This is c language and I have a file which I want to read it tothis array of pointers to structure. I have 2 doctors and 3patients, and the clinic I want to read and store it to thestructure objects, so this is how it is:#include<stdio.h>
struct doctors
{
char firstname[20];
char lastname[20];
int operationid;
};
struct patients
{
char firstname[20];
char lastname[20];
int chosendocid;
};
struct clinic
{
char clinicname[25];
struct doctors *ptr[2]; //an array of pointers to objects of the doctor structure
struct patients *pat[3] //an array of pointers to objects of the patient structure
//I can only use this array of pointers.
};
void recursivealloc_mem(FILE *notefile, struct patients, intrec);
int main(void)
{
FILE *notefile;
struct clinichosp; //I can only declare this two localvaluables,
int rec=2;
notefile =fopen("file1.dat", "r");
if(notefile == NULL)
{
printf("file1.dat file does not exist ");
return(-1);
}
recursivealloc_mem( notefile, &hosp, rec);
fclose(notefile);
}
void recursiveallocdoc_mem(FILE *notefile, struct clinic *xhosp,int rec)
{
int index, xindex =1;
if(xindex <= rec)
recursiveallocdoc_mem(rec - 1);
xhosp->doc[rec] = malloc(3 * sizeof( struct doctor));
for(index=0;index<2;index++)
{
fscanf(notefile, "%s %s %d ",xhosp->doc[i]->firstname,xhosp->doc[i]->lastname, xhosp->doc[i]->id);
printf("%s, %s,%d ",xhosp->doc[i]->firstname,xhosp->doc[i]->lastname, xhosp->doc[i]->id);
}
}
}
And the file I have written so far is:
James
Warren
3542
Maud
Spearman
2267
Ike
Williams
2267
Caleb
Walker
3542
I am trying to let the program read and store it by allocatingmemory recursively, that's the one it's giving me problems becauseI don't understand array of pointers to structures very well.Please I can only declare the structure objects like it is. Soplease how can I recursively allocate memory for it?
Explanation / Answer
intrec=3;
notefile = fopen("file1.dat", "r");
if(notefile == NULL)
{
printf("file1.dat file does not exist ");
return(-1);
}
recursiveallocdoc2_mem(notefile,doc_list,0);
recursiveallocdoc1_mem(notefile,pat_list,0);
recursivealloc_mem(notefile,hosp, 0);
fclose(notefile);
for(i=0;i<2;i++)
{
printf("clinic name %s :Patient %d: Patient name:%s %s doctor =%s %s withoperationid=%d ",hosp->clinicname,i+1,hosp[i]->pat->firstname,hosp[i]->pat->lastname,
hosp[i]->ptr->firstname,hosp[i]->ptr->lastname,hosp[i]->ptr->operationid);
}
system("pause");
}
void recursiveallocdoc2_mem(FILE *notefile, struct doctors **xhosp,int rec)
{
char str[20];
struct doctors*hosp;
if(rec < 2){
hosp = (struct doctors*)malloc(sizeof( struct doctors));
fgets((hosp)->firstname,20,notefile);
fgets((hosp)->lastname,20,notefile);
fgets(str,20,notefile);
hosp->operationid = atoi(str);
*xhosp = hosp;
recursiveallocdoc_mem(notefile,xhosp+1,rec+1);
}
}
voidrecursiveallocdoc1_mem(FILE *notefile, struct patients**xhosp, intrec)
{
char str[20];
struct patients*hosp;
if(rec < 2){
hosp = (struct patients*)malloc(sizeof( struct patients));
fgets((hosp)->firstname,20,notefile);
fgets((hosp)->lastname,20,notefile);
fgets(str,20,notefile);
hosp-> chosendocid = atoi(str);
*xhosp = hosp;
recursiveallocdoc_mem(notefile,xhosp+1,rec+1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.