簡體   English   中英

如何從兩個文件中讀取和打印信息?

[英]How do i read and print information from two files?

我的班級、我的老師和我都無法弄清楚代碼有什么問題。 這是一個食堂點餐系統,所以作為一個學生,你點了一些東西,它會記錄到一個系統中,食堂工作人員可以相應地制作食物。 具體問題是我正在嘗試進行統計,但它只打印訂購的第一個食品。 相關代碼如下,並附有圖片。

可用項目菜單,已下訂單訂單統計結果//注意學生的訂單是食品項目的 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);
}

這是由我的最高評論開始的:

您正在執行for (i=1;i<=100;i++)並且該循環的第一部分是對fp1的全部內容進行fread [在while循環中]。 因此,當i為 2 時, fread將立即獲得 EOF。 您是否需要在for循環的每次迭代中為fp1 [倒帶] 進行fseek 如果不是,那么fp1while最好放在 for 循環之上。 事實上,重讀文件 100 次似乎很浪費。

我對事情進行了一些重組和簡化。 一次讀取學生文件並將所有總和記錄在一個數組中容易得多。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM