因此,我的fprintf和fput未能成功写入所需的文件中。这是相关的代码,谢谢。
void print_stats(double max, double min, double avg, double sum)
{
FILE *paid = ("paid.txt", "w");
//paid = ("paid.txt", "w");
if (paid == NULL)
printf("Failed");
fputs("Test", paid);
fprintf(paid, "Max: %.2f\nMin: %.2f\nAverage: %.2f\nTotal: %.2f",
max, min, avg, sum);
fclose(paid);
}
答案 0 :(得分:4)
您从未真正打开过文件:
FILE *paid = ("paid.txt", "w");
这将("paid.txt", "w")
作为表达式求值,逗号运算符将丢弃左操作数“ paid.txt”并求值右操作数“ w”,然后将其分配给paid
。
您需要在此处致电fopen
:
FILE *paid = fopen("paid.txt", "w");