【发布时间】:2017-12-02 16:18:52
【问题描述】:
在这个 C 程序中,我有一个包含重复学生 ID 的数组。当我尝试 将其写入文本文件,它会跳过重复的值并写入 剩余数据放入文件中,每个重复值中的值为 0。但是我 想要将该重复记录也写入文件中。
这是我的代码;
int WriteData(struct Sales StoreSales[])
{
int i;
FILE *fptr;
fptr = fopen("reverseOrder.txt", "w");
if(fptr == NULL)
{
printf("\nError: File cannot be opened\n");
return -1;
}
fprintf(fptr, "\nStudent ID\tSales Amount\n");
for(i = SIZE ; i > 0 ; i--)
{
fprintf(fptr, "%d\t\t\t%d\n", StoreSales[i].StudentID,
StoreSales[i].amount );
}
fclose(fptr);
}
Here's my array;
301 -> 4
201 -> 3
657 -> 4
234 -> 9
301 -> 8
201 -> 4
因为我是 C 的初学者,所以我找不到解决这个问题的方法。任何 有用的想法来修复我的代码?谢谢!
【问题讨论】:
-
你能显示预期的输出吗?
-
我想用所有重复值以相反的顺序写入给定的数组。 @coderredoc
-
你能显示你得到的输出吗?
-
请发布整个程序。
-
是的。我可以编译和执行。 @hashini.W