I have a txt file which contains patient details separated by commas
I want to read each value store that in a structure. But, the problem is that some of the entries contain 3 values and the others contain 4.
ENTRIES IN TXT FILE are:
1032,Pugsley Yanson,CELL,3048005191
1048,Banjo Codi,TBD,
1056,Lettuce Peas,WORK,7934346809
My Code looks like :
`struct Phone
{
char description[PHONE_DESC_LEN];
char number[PHONE_LEN];
};
// Data type: Patient
struct Patient
{
int patientNumber;
char name[NAME_LEN];
struct Phone phone;
};
void importPatients(const char* datafile, struct Patient patients[], int max){
FILE *fp = fopen(datafile, "r");
int i = 0;
int read = 0;
while (!feof(fp) && i < max){
read = fscanf(fp,"%d,%14[^,],%4[^,],%10[^,]\n",&patients[i].patientNumber,patients[i].name,patients[i].phone.description,patients[i].phone.number);
if(read == 0 && !feof(fp)){
fclose(fp);
return;
}
i++;
}
fclose(fp);
}
This code works perfectly when reading entries with 4 values but fails as soon as it encounters an entry with 3 values like: 1048,Banjo Codi,TBD,
How can this be fixed or is there a better approach to solve this problem?