
Patrick B. answered 05/18/21
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PATIENT_STR_SIZE (18)
#define PATIENT_OUTBUFF_LENGTH (55)
typedef struct _TPatient
{
char socialSecurityNumber[PATIENT_STR_SIZE];
char dob[PATIENT_STR_SIZE];
double balance;
long accountNum;
} * TPatient;
Patient_Input(TPatient patientRec)
{
printf(" Please input the social security number XXX-XX-XXXX :>");
scanf(patientRec->socialSecurityNumber);
printf(" Please input the date of birth mm/dd/yyyy :>");
scanf(patientRec->dob);
printf(" Please input the current balance XXXXXX.XX :> ");
scanf("%lf",&patientRec->balance);
printf(" Please input the account # :> ");
scanf("%ld",&patientRec->accountNum);
}
Patient_SerializeToCSV(TPatient patientRec,char * patientRecBuff)
{
sprintf(patientRecBuff,"%s,%s,%8.2lf,%ld\n",patientRec->socialSecurityNumber,patientRec->dob,patientRec->balance,patientRec->accountNum);
}
Patient_Parse(TPatient patientRec,char * patientRecBuff)
{
strcpy(patientRec->socialSecurityNumber,strtok(patientRecBuff,","));
strcpy(patientRec->dob,strtok(NULL,","));
patientRec->balance = atof(strtok(NULL,","));
patientRec->accountNum = atoi(strtok(NULL,","));
}
Patient_Dump(TPatient patientRec,char * strMsg)
{
if (strMsg!=NULL)
{
printf("**********************************************\n");
printf(strMsg); printf("\n");
}
printf("***********************************************\n");
printf(" social security # >%s< \n",patientRec->socialSecurityNumber);
printf(" dob >%s< \n",patientRec->dob);
printf(" balance >%6.2f< \n",patientRec->balance);
printf(" account # >%ld< \n",patientRec->accountNum);
}
int ReadPatients(TPatient patients, int n, char * filename)
{
int N, // # of patient recs in the file
numRecs, // # of patient recs that shall be read
iLoop; //generic loop counter
char inbuff[PATIENT_OUTBUFF_LENGTH]; //input buffer
FILE * fptr = fopen(filename,"r"); //opens the file
int iReturn=0;
if (fptr!=NULL)
{
//reads # of patient recs in the file
fscanf(fptr,"%d",&N);
numRecs = (n>N) ? N : n; //numrecs contains the SMALLER of n and N
// EX. file has 10 recs, n=5 ---> reads 5 recs;
// EX. file has 5 recs, n=10 --> reads 5 recs
fgets(inbuff,PATIENT_OUTBUFF_LENGTH,fptr); //eats the newline
printf(" incoming.... \n");
for (iLoop=0; iLoop<numRecs; iLoop++)
{
fgets(inbuff,PATIENT_OUTBUFF_LENGTH,fptr); //eats reads the patient record
Patient_Parse(&patients[iLoop],inbuff);
Patient_Dump(&patients[iLoop],NULL);
}
fclose(fptr);
}
else
{
iReturn=-1;
}
return(iReturn);
}
int WritePatients(TPatient patient, int n, char * filename)
{
int iLoop, iReturn=0;
FILE * fptr = fopen(filename,"w");
char outbuff[PATIENT_OUTBUFF_LENGTH];
if (fptr!=NULL)
{
fprintf(fptr,"%d \n",n);
for (iLoop=0; iLoop<n; iLoop++)
{
Patient_SerializeToCSV(&patient[iLoop],outbuff);
fputs(outbuff,fptr);
}
fclose(fptr);
}
else
{
iReturn=-1;
}
return(iReturn);
}
int main()
{
struct _TPatient patients[4];
int iReturn = ReadPatients(patients,4,"E:\\patients.dat"); //you will have to change these path and file names
if (iReturn==0)
{
WritePatients(patients,4,"E:\\patients.txt");
}
else
{
printf(" Error reading file \n");
}
}
/*****************************************************
THIS WORKS!!!!
int main()
{
struct _TPatient patientRec;
char patientRecBuff[] = "123-45-6789,01/02/03,1234.56,24680";
char outbuff[PATIENT_OUTBUFF_LENGTH];
Patient_Parse(&patientRec,patientRecBuff);
Patient_Dump(&patientRec,(char*)"PARSED");
Patient_SerializeToCSV(&patientRec,outbuff);
printf(" serialzied outbuff >%s< \n",outbuff);
Patient_Parse(&patientRec,outbuff);
Patient_Dump(&patientRec,(char*)"PARSED FROM OUTBUFF");
}
****************************************************/
Here's the file:
4
123-45-6789,01/02/2003,1234.56,24680
987-65-4321,04/05/2006,2345.67,13579
246-80-1234,07/08/2010,1357.99,80462
135-79-4321,09/11/2013,8642.99,97531