
Patrick B. answered 05/20/21
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PATIENT_STR_LENGTH (12)
typedef struct _TPatient
{
char socialSecurityNum[PATIENT_STR_LENGTH];
char dob[PATIENT_STR_LENGTH];
int status;
} * TPatient;
#define PATIENT_SIZE (sizeof(struct _TPatient))
typedef struct _TPatientDB
{
TPatient patients;
int count;
} * TPatientDB;
int Patient_Input(TPatient patient)
{
printf(" Please input the patient social security # :>");
scanf("%s",patient->socialSecurityNum);
printf("Please input the date of birth :>");
scanf("%s",patient->dob);
int iStatus=-1;
while (iStatus<0)
{
printf(" Please input the patient status :>");
scanf("%d",&iStatus);
}
patient->status = iStatus;
}
void Patient_Output(TPatient patient, char * strMsg)
{
if (strMsg!=NULL)
{
printf("---------------------------------------------------\n");
printf(strMsg);
printf("\n");
}
printf("--------------------------------------------\n");
printf(" soc sec # >%s< \n",patient->socialSecurityNum);
printf(" dob >%s< \n ", patient->dob);
printf(" status = %d \n",patient->status);
}
void Patient_Parse(TPatient patient, char * patientRecBuffCSV)
{
strcpy(patient->socialSecurityNum,strtok(patientRecBuffCSV,","));
strcpy(patient->dob,strtok(NULL,","));
patient->status = atoi(strtok(NULL,","));
}
void Patient_Read(char * filename, int n, TPatientDB patientDB)
{
FILE * fptr = fopen(filename,"r");
char inbuff[255];
int numPatients;
struct _TPatient curPatientRec;
char strMsg[25];
if (fptr!=NULL)
{
//reads the # of patient records from the file and eats the newline
fscanf(fptr,"%d",&patientDB->count); fgets(inbuff,255,fptr);
numPatients = patientDB->count;
patientDB->patients = (TPatient)malloc(PATIENT_SIZE*numPatients);
for (int iLoop=0; iLoop<numPatients; iLoop++)
{
fgets(inbuff,255,fptr);
Patient_Parse(&curPatientRec,inbuff);
memcpy(&patientDB->patients[iLoop],&curPatientRec,PATIENT_SIZE);
if (iLoop<n)
{
sprintf(strMsg,"PATIENT REC # %d of %d \n",(iLoop+1),numPatients);
Patient_Output(&patientDB->patients[iLoop],strMsg);
}
}
fclose(fptr);
}
else
{
patientDB->count=-1;
patientDB->patients = NULL;
}
}
void Patient_SerializeToCSV(TPatient patient,char * patientRecBuffCSV)
{
sprintf(patientRecBuffCSV,"%s,%s,%d",patient->socialSecurityNum,patient->dob,patient->status);
}
void Patient_Write(char * filename, int n, TPatientDB patientDB)
{
FILE * fptr;
fptr = fopen(filename,"w");
char outbuff[255];
int iLoop;
fprintf(fptr,"%d\n",patientDB->count);
for (int iLoop=0; iLoop<patientDB->count; iLoop++)
{
Patient_SerializeToCSV(&patientDB->patients[iLoop],outbuff);
fprintf(fptr,"%s\n",outbuff);
}
fclose(fptr);
}
PatientDB_Destroy(TPatientDB patientDB)
{
if (patientDB->patients!=NULL)
{
free(patientDB->patients);
}
patientDB->count=0;
}
PatientDB_Input (TPatientDB patientDB)
{
int n=-1;
int iLoop;
char filename[255];
while (n<0)
{
printf(" How many patients ??? :>");
scanf("%d",&n);
}
patientDB->count=n;
patientDB->patients = (TPatient)malloc(PATIENT_SIZE*n);
for (iLoop=0; iLoop<n; iLoop++)
{
Patient_Input(&patientDB->patients[iLoop]);
}
printf("Filename :>"); scanf("%s",filename);
Patient_Write(filename,n,patientDB);
}
int main()
{
struct _TPatientDB patientDB;
struct _TPatientDB dbPatients;
Patient_Read((char*)"E:\\patients.dat",4,&patientDB);
PatientDB_Input(&dbPatients);
PatientDB_Destroy(&patientDB);
PatientDB_Destroy(&dbPatients);
}