I am trying to create a method in C so I can write to a file but I am not sure why it's not working. Is there anything wrong with my code?
Thank you in advance for your help.
void writeToFile(Employee *employeeRecords, int count) {
FILE * f = NULL;
fopen("employeeData.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
const char *text = "Employee data: \n";
fputs(text, f);
for(int l = 0; l < count; l++) {
fprintf(f, "%c", employeeRecords[l].name);
fprintf(f, "%c", employeeRecords[l].surname);
fprintf(f, "%d", employeeRecords[l].age);
fprintf(f, "%lf", employeeRecords[l].salary);
}
fclose(f);
}
Try:
f=fopen("employeeData.txt", "w");