我想在C语言中创建一个方法,这样我就可以写到一个文件,但我不知道为什么它不能工作。我的代码有什么问题吗?
先谢谢你的帮助。
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);
}
尝试。
f=fopen("employeeData.txt", "w");