Вот как это должно быть сделано (я вставил заметки о проблемах в коде)
char answer;
// NOTE: this must be outside the loop
int i=0;
do
{
printf(" Enter details of patient %d\n\n", i+1);
printf(" Patients first name: ");
scanf("%s",arr_patient[i].fname);
printf(" Last name: ");
scanf("%s",arr_patient[i].lname);
printf(" Personnummer: ");
scanf("%d", &arr_patient[i].birthdate);
printf(" Bildref: ");
scanf("%d", &arr_patient[i].bildref);
printf(" Do you want to add someone else?: (y/n):");
scanf(" %c",&answer);
i++;
}while(answer != 'n');
FILE *patientfile = fopen("test.txt", "a");
if (!patientfile) printf(" Failed to open file.\n");
// NOTE: You need to loop over the array and write all the patients to the file
for(int j=0; j<i; j++)
{
fprintf(patientfile," %s\t%s \n ", arr_patient[j].fnamn,arr_patient[j].lnamn);
fprintf(patientfile," %d \n",arr_patient[j].birthdate);
fprintf(patientfile," %d \n",arr_patient[j].bildref);
}
fclose(patientfile);
Но на самом деле вам не нужен массив. Вы можете записать пациентов в файл прямо так:
FILE *patientfile = fopen("test.txt", "a");
if (!patientfile) printf(" Failed to open file.\n");
char answer;
int i=0;
do
{
// change patient_struct to your structure name
patient_struct patient;
printf(" Enter details of patient %d\n\n", i+1);
printf(" Patients first name: ");
scanf("%s",patient.fname);
printf(" Last name: ");
scanf("%s",patient.lname);
printf(" Personnummer: ");
scanf("%d", &patient.birthdate);
printf(" Bildref: ");
scanf("%d", &patient.bildref);
fprintf(patientfile," %s\t%s \n ", patient.fnamn, patient.lnamn);
fprintf(patientfile," %d \n", patient.birthdate);
fprintf(patientfile," %d \n", patient.bildref);
printf(" Do you want to add someone else?: (y/n):");
scanf(" %c",&answer);
i++;
}while(answer != 'n');
fclose(patientfile);