[英]How do i read and print information from two files?
My class, my teacher and I all couldn't figure out what's wrong with the code.我的班级、我的老师和我都无法弄清楚代码有什么问题。 It's a canteen ordering system, so as a student you order something and it logs it into a system and the canteen workers can make food accordingly.
这是一个食堂点餐系统,所以作为一个学生,你点了一些东西,它会记录到一个系统中,食堂工作人员可以相应地制作食物。 The specific problem is that I'm trying to make a tally but it only prints the first food item that was ordered.
具体问题是我正在尝试进行统计,但它只打印订购的第一个食品。 The relevant code is below and images are attached.
相关代码如下,并附有图片。
Menu of available items , Orders Placed , Result of order tally //note the student's order is an ID of the food item可用项目菜单,已下订单,订单统计结果//注意学生的订单是食品项目的 ID
struct food
{
int foodID;
char foodName[namesize];//namesize is 30
char foodItems[size];//specials for the day//size is 3
int itemTally;//total of food that needs to be prepared - REPLACED
float totalEarnt;//total amount earnt for selling the food
float totalSpen;//cost of food to the employer
float totalProfits;//total profts made
float itemCost;//cost of the food item
};
struct student
{
char firstname[namesize];
char lastname[namesize];
int ID;//students ID
float form;//student's form room
int order;//ID of the food they ordered
int quantity;//how much of the food item
float paid;//how much they paid
int delivery;//This is a yes or no
char location[aSize];//where it will be delivered to; aSize is 20
};
void orderTally()//Tallies up all the orders based on their ID; Student 1 orders 3 fish, student 2 orders 4 fish, outputs 7 fish
{
FILE*fp;
FILE*fp1;
struct food foodie={0,"","",0,0.0,0.0,0.0,0.0};
struct student orderie={"","",0,0.0,0,0,0.0,0,""};
int i;//This is the food ID.
if((fp1 = fopen("student", "rb+"))==NULL)//order file
{
printf("Cannot open order file \n");
exit(1);
}
if ((fp = fopen("food", "rb+"))==NULL)//menu file
{
printf("Cannot open food file \n");//UPDATE: update other statements
exit(1);
}
int sum=0;
for(i=1;i<=100;i++)
{
fread(&orderie,sizeof(struct student),1,fp1);
while(!feof(fp1))
{
if(orderie.order==i)//If the order ID is equal to one of the 100 food IDs...
{
sum=sum+orderie.quantity;
}
fread(&orderie,sizeof(struct student),1,fp1);
}
if(sum!=0)
{
fseek(fp,(i-1)*sizeof(struct food),SEEK_SET);
fread(&foodie,sizeof(struct food),1,fp);
printf("------------------------------------------------------------\n");
printf("\t\t\tORDERS\n");
printf("------------------------------------------------------------\n\n");
printf("%s",foodie.foodName);
printf(" - %d\n\n",sum);
sum=0;
}
}
fclose(fp1);
fclose(fp);
}
This is prefaced by my top comment:这是由我的最高评论开始的:
You are doing
for (i=1;i<=100;i++)
and the first part of that loop is doingfread
on the entire contents offp1
[in thewhile
loop].您正在执行
for (i=1;i<=100;i++)
并且该循环的第一部分是对fp1
的全部内容进行fread
[在while
循环中]。 So, wheni
is 2, thefread
will get an immediate EOF.因此,当
i
为 2 时,fread
将立即获得 EOF。 Do you need anfseek
forfp1
[to rewind it] on each iteration of thefor
loop?您是否需要在
for
循环的每次迭代中为fp1
[倒带] 进行fseek
? If not, thewhile
forfp1
would be better placed above the for loop.如果不是,那么
fp1
的while
最好放在 for 循环之上。 As it is, it seems wasteful to reread the file 100 times.事实上,重读文件 100 次似乎很浪费。
I've restructured and simplified things a bit.我对事情进行了一些重组和简化。 It's a lot easier to read the student file once and record all sums in an array.
一次读取学生文件并将所有总和记录在一个数组中要容易得多。
struct food {
int foodID;
char foodName[namesize]; // namesize is 30
char foodItems[size]; // specials for the day//size is 3
int itemTally; // total of food that needs to be prepared - REPLACED
float totalEarnt; // total amount earnt for selling the food
float totalSpen; // cost of food to the employer
float totalProfits; // total profts made
float itemCost; // cost of the food item
};
struct student {
char firstname[namesize];
char lastname[namesize];
int ID; // students ID
float form; // student's form room
int order; // ID of the food they ordered
int quantity; // how much of the food item
float paid; // how much they paid
int delivery; // This is a yes or no
char location[aSize]; // where it will be delivered to; aSize is 20
};
// Tallies up all the orders based on their ID; Student 1 orders 3 fish,
// student 2 orders 4 fish, outputs 7 fish
void
orderTally()
{
FILE *fp;
FILE *fp1;
struct food foodie;
struct student orderie;
int i; // This is the food ID.
// order file
if ((fp1 = fopen("student", "rb+")) == NULL)
{
printf("Cannot open order file \n");
exit(1);
}
int sums[101] = { 0 };
// read _entire_ student file in one pass
while (1) {
if (fread(&orderie,sizeof(struct student),1,fp1) != 1)
break;
sums[orderie.order] += orderie.quantity;
}
fclose(fp1);
// menu file
if ((fp = fopen("food", "rb+")) == NULL)
{
printf("Cannot open food file \n"); // UPDATE: update other statements
exit(1);
}
for (i = 1; i <= 100; i++) {
int sum = sums[i];
if (sum == 0)
continue;
fseek(fp, (i - 1) * sizeof(struct food), SEEK_SET);
fread(&foodie, sizeof(struct food), 1, fp);
printf("------------------------------------------------------------\n");
printf("\t\t\tORDERS\n");
printf("------------------------------------------------------------\n\n");
printf("%s", foodie.foodName);
printf(" - %d\n\n", sum);
}
fclose(fp);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.