c - 即使我正在保存,数据库也不会在 C 上输出

标签 c output

我正在做一个订单的数据库,在这种情况下处理交易。

过程如下:

  1. 首先,客户输入他的 ID。如果未找到,则退出到主菜单,否则继续(客户必须在数据库中注册一个 ID,该 ID 存储在另一个文件中)

  2. 它会询问他们要购买哪种产品(它会询问产品名称 [这就是讲师想要的名称],如果找不到它,则它存在,否则它会继续

  3. 它会询问他们要购买多少,并检查是否有足够的库存。如果是则继续,否则询问用户是否要输入另一个数字或退出

  4. 自动计算价格并询问确认。如果客户确认,则保存完成,否则退出主菜单

现在我的问题是,即使我的 STRUCT 正在保存,每当我在数据库中输出任何订单时(现在测试,因为我首先需要客户的最后订单)数据库总是显示为空。下面是Coding【有点长,不好意思,看不懂哪里不对】,也会提供List All的功能。

还提供了屏幕截图以更好地了解程序的工作原理:

void customerOrder()
{
    int checkID = 0; //variable to hold the ID input
    char ch;
    char ch1;
    char ch2;
    char option;
    char tempName [100];
    int  order = 0;
    int  tempStock = 0;
    float tempPrice = 0;



    printf ("\n\n\n\n\t\t    ************* Add Customer Order *************\n \n \n");

    // ----------- LOADING OF THE 3 DATA FILES -----------//
    if ((ofp = fopen ("orders.dat","a+b")) == NULL)
    {
        fputs   ("Error! Cannot open orders.dat\n",stderr);
        system  ("PAUSE");
        orderMainMenu();
    }
    rewind (ofp);

    if ((cfp = fopen ("customers.dat","r"))== NULL)
    {
        fputs ("Error! Cannot open customers.dat\n",stderr);
        system ("PAUSE");
        orderMainMenu();
    }
    rewind (cfp);

    if ((pfp = fopen ("products.dat","r+b"))== NULL)
    {
        fputs ("Error! Cannot open products.dat\n",stderr);
        system ("PAUSE");
        orderMainMenu();
    }
    rewind (pfp);

    //-------- Confirm whether to start Order ------------//
    printf ("WARNING: In order for an Order to be made, the Customer must be in the Database\n");
    printf ("Are you sure you want to continue? Y or N\n");
    while (getchar() !='\n')
    {
    }
    ch1 = getchar ();
    if (ch1 == 'Y' || ch1 == 'y')
    {
        // ---- INPUT OF CUSTOMER ID --------------//
        printf ("\nPlease Enter ID: ");

            while (scanf ("%d",&checkID) == 0)
            {
                printf ("\n\nInvalid Input!!!\n");
                printf ("Either you have entered a Letter!!\n");
                printf ("Press 'Y' to enter another ID or any key to return to MainMenu\n\n");
                while (getchar()!='\n')
                    {
                    }
                option = getchar();
                if (option == 'Y' || option == 'y')
                {
                    printf ("\nPlease Enter Another ID Number:\n");
                }
                else
                {
                    printf ("\nReturning to Order Management Menu\n");
                    system ("PAUSE");
                    fflush(stdin);
                    orderMainMenu();

                }
            }
            //---------- CHECK WHETHER ID EXISTS OTHERWISE EXIT TO MENU --------------//
            while (fread (&c, STRUCTSIZEC,1,cfp) == 1)
            {
                if (c.ID == checkID)
                {

                    clrscr();
                    printf ("\n\n\n\n\t\t    ************* Add Customer Order *************\n \n \n");

                    // SHOWS WHICH ID IS BEING SERVED //
                    printf ("\n\nNew Order For ID: %d\n", c.ID);


                    // ASKS WHICH PRODUCT TO BUY //
                    printf ("\nWhich Product do you want to buy?\n\n");
                    printf ("WARNING! Product Name is CASE SENSITIVE:\n");

                    // INPUT NAME //
                    printf ("Product Name: ");
                    while (getchar() !='\n')
                    {
                    }

                    fgets  (tempName, 100, stdin);
                    while (fread (&p, STRUCTSIZEP,1,pfp)== 1)
                    {
                        if (strncmp (tempName,p.pName,sizeof(tempName)) == 0)
                        {


                    // --- SHOWING ID and WHICH PRODUCT CUSTOMER IS GOING TO BUY -- //
                    clrscr ();
                    printf ("\n\n\n\n\t\t    ************* Add Customer Order *************\n \n \n");
                    printf ("Order for ID: %d\n", c.ID);
                    printf ("Product Name: %s\n\n", p.pName);

                    tempStock = p.pStock;

                    printf ("How many do you wish to buy?\n");
                    printf ("Currently there is %d in Stock", tempStock);
                    printf ("Order:  ");

                    while (scanf ("%d",&order) == 0)
                    {
                        printf ("Invalid Order! Only Numbers are  allowed!\n");
                        while (getchar() !='\n')
                        {
                        }
                    }
                    //---- CHECK WEHTHER ORDER IS BIGGER THAN WHAT IS FOUND IN STOCK ----//
                    //---- IF YES ASK IF USER WANTS TO INPUT ANOTHER NUMBER OR EXIT ----//
                    while (order > tempStock)
                    {
                        printf ("There is not enough items in Stock to satisfy that quantity!\n");
                        printf ("Do you want to enter another quantity? 'Y' for yes, any key to return to Menu\n");
                        fflush (stdin);
                        ch2 = getchar();
                        if (ch2 == 'Y' || ch2 == 'y')
                        {
                            printf ("Please enter another quantity:\n");
                            scanf ("%d",&order);
                        }
                        else
                        {
                            printf ("Order Canceled! Returning to Main Menu");
                            system ("PAUSE");
                            fclose (cfp);
                            fclose (ofp);
                            fclose (pfp);
                            orderMainMenu();
                        }
                    }

                    printf ("\nTotal Price for this Order will be:\n");
                    tempPrice = (order * p.pPrice);
                    printf ("Total: %.2f\n", tempPrice);

                    // ---- SHOW THE TRANSACTION OF THE USER AND ASK WHETHER TO CONFIRM ---- //
                    clrscr();
                    printf ("\n\n\n\n\t\t    ************* Add Customer Order *************\n \n \n");

                    printf ("This is the Customer's Overview of Purchase:\n\n");
                    printf ("Customer's ID: %d\n",c.ID);
                    printf ("Customer's Product: %s",p.pName);
                    printf ("Order: %d\n",order);
                    printf ("Total Price: %.2f\n\n",tempPrice);

                    printf ("\n\n----------------------------------------\n\n");
                    printf ("Are you sure you of this transaction?\n");
                    printf ("Warning: After Confirming you cannot change the Order!\n");
                    printf ("Press 'Y' to confirm the Transaction otherwise press 'N' to cancel the order and return to Main Menu\n");

                    while (getchar() !='\n')
                    {
                    }

                    ch = getchar();
                    if (ch == 'N' || ch == 'n')
                     {
                         printf ("Transaction CANCELLED! Returning to Order Main Menu!\n");
                         system ("PAUSE");
                         orderMainMenu();
                     }
                     else if (ch == 'y' || ch == 'Y')
                     {
                         tempStock = (tempStock - order);
                         p.pStock = tempStock; //Updates the new stock number in Products' Database
                         fseek (pfp,-STRUCTSIZEP,SEEK_CUR);
                         fwrite(&p, STRUCTSIZEP,1,pfp);
                         fclose (pfp);

                         o.quantity = order;
                         o.cID = c.ID;
                         o.price = tempPrice;
                         strncpy(o.pName,p.pName, sizeof(p.pName));
                         o.timer = time(NULL);

                         fwrite (&o,STRUCTSIZEO,1,ofp);
                         fclose (ofp); //Closing of Files
                         fclose (cfp);
                         fclose (pfp);

                         printf("The Transaction Order saved is as follows:\n");
                         printf("ID: %d\nProduct: %sQuantity: %d\nPrice: %.2f\n",o.cID,o.pName,o.quantity,o.price);
                         printf("Transaction Made at: %s\n",asctime(localtime(&o.timer)));
                         system ("PAUSE");
                         orderMainMenu();
                     }
                }
            }
        }
    }
    }
    else
    {
        printf ("Returning to Order Main Menu\n");
        system ("PAUSE");
        orderMainMenu();
    }
}

ListAll 方法:

void oListAll()
{
    order o;

    printf ("\n\n\n\n\t\t    ********** Current Products in the Database *******\n \n \n");

    //--------------- LOADING OF FILE ------------ //
    if ((ofp = fopen ("orders.dat","rb")) == NULL)
    {
        fputs ("Cannot open products.dat file!\n",stderr);
        printf ("Returning to Order Main Menu\n");
        system ("PAUSE");
        orderMainMenu();
    }
    rewind (ofp);

    // --------- START TO TRAVERSE THE DATABASE AND OUTPUT DATA -------- //
    printf ("Current Orders in the Database:\n");
    while (fread (&o, STRUCTSIZEO,1,pfp)==1)
    {
        printf (" Name: %s Price: %.2f\n In Stock: %d\n\n", o.pName, o.price, o.quantity);
    }
    system ("PAUSE");
    productMainMenu();

}

这些是屏幕截图:

  1. http://tinypic.com/r/110x7c2/6
  2. http://tinypic.com/r/1446ya/6
  3. http://tinypic.com/r/315iy3s/6
  4. http://tinypic.com/r/15xo4lt/6
  5. http://tinypic.com/r/2ze9wfr/6
  6. http://tinypic.com/r/jtx8xw/6

我知道它相当长,但请耐心等待,我已经花了 4 个多小时试图弄清楚它出了什么问题。非常感谢

最佳答案

您的 oListAll() 函数打开 FILE *ofp,但从 pfp 中读取。尝试从 ofp

读取

关于c - 即使我正在保存,数据库也不会在 C 上输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14055713/

相关文章:

windows - 如何将命令行指令本身转储到输出文件中?

c - 如何在格式化字符串上使用 strlen()?

c++ - 如何使用 FFmpeg 和 C/C++ 将数据流添加到 MXF(使用 mpeg2video)文件中

c - 为什么我的代码在运行时输出 0?怎么了,我该如何解决?

java - 如何删除重复值然后显示唯一值?

Hadoop Mapreduce MultipleOutputs 输出控制台

c - tm 结构(来自 time.h)如何工作?

c++ - 从视频纹理中选择源矩形

c++ - C++11 中具有 C 链接的复杂类型

python - 如何在CMD中隐藏yt-dl的输出? Python