显示结果

时间:2019-04-25 20:37:02

标签: c randomaccessfile

我能够将数据输入到程序中,并使用一些printf语句来查看数据是否被正确写入。当我要打印一个记录或所有记录时,该程序将强制退出并退出(dispAll)或变得乱七八糟,并且即使我基于该键前往同一位置,也找不到找到的记录。

我添加了很多打印语句来查看记录中的值,但是混合结果决定了丢失数据的位置。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

 struct Record
   {
      int available;
      int itemID;
      char itemName [16];
      char itemDescription [31];
      double itemUnitPrice;
      int itemOnHand;
      double value;
      int key;
   };

// function prototypes
void displayMenu ();
int getChoice ();
void processChoice (int pChoice);
void addRecord();
void modRecord ();
void dispOneRecord ();
void dispAllRecords ();
void dispItem (struct Record inventory);


int main (void)
{                                //opens the function main

// Local Declarations
   int pChoice;
   struct Record inventory;
   int i;
   FILE* fp;
   int ava = 0;

// Statements

   // creates a file to be used by the program
   fp = fopen ("invent.txt", "wb+");

   for (i = 0; i < 23; i++)
   {
       fseek(fp, i * sizeof(struct Record), SEEK_SET);

       fwrite(&ava, sizeof(int), 1, fp);
   }

   fclose (fp);

    do 
      {                          // begins do loop

         displayMenu();

         pChoice = getChoice();

         processChoice (pChoice); 

      } 
   while (pChoice != 5);       // ends do loop

return(0);

} // closes main

void displayMenu ()
{

   printf("\n ***********************************************************\n"); 
   printf("\n               Inventory Management System                  \n");
   printf("\n Select the number for the action you would like to conduct.\n");     

   printf("\n 1 - Add Inventory Records to File");
   printf("\n 2 - Modify Data in an Inventory Record");
   printf("\n 3 - Display One Item and Value");
   printf("\n 4 - Report all Inventory and the Total Value");
   printf("\n 5 - Exit the Program\n\n");

   printf("\n ***********************************************************\n"); 

 return;
}

int getChoice ()
{

   // local variables
   int pChoice;

   // prompts the user for their choice

   printf("\nPlease enter your choice: ");

   scanf("%d", &pChoice);

   return(pChoice);
}

void processChoice (int pChoice)
{

   switch (pChoice)
   {

      case 1:
           addRecord();
           break;

      case 2:
           modRecord();
           break;

      case 3:
           dispOneRecord();
           break;

      case 4: 
           dispAllRecords();
           break;
   }
return;
}

void addRecord()
{

   //Local Variables
   FILE* fp;
   struct Record inventory;

   printf("\nPlease enter the Item Identification Number: ");
   scanf("%d", &inventory.itemID);

   printf("\nPlease enter the Item Name (Limit of 15 Characters): ");
   scanf("%s", inventory.itemName);

   printf("\nPlease enter the Item Description (Limit of 30 Characters): ");
   scanf("%s", inventory.itemDescription);

   printf("\nPlease enter the Item Unit Price: $");
   scanf("%lf", &inventory.itemUnitPrice);

   printf("\nPlease enter the number of Items on Hand: ");
   scanf ("%d", &inventory.itemOnHand);

   inventory.value = inventory.itemUnitPrice * inventory.itemOnHand;  

   fp = fopen("invent.txt", "rb+");

   inventory.key = inventory.itemID % 23;  

printf ("\n%d", inventory.key);

   fseek(fp, inventory.key * sizeof(struct Record), SEEK_SET);

   fread (&inventory.available, sizeof(int), 1, fp);

printf ("\n%d", inventory.available);

   if (inventory.available == 0)

   {

      fseek(fp, inventory.key * sizeof(struct Record), SEEK_SET);

      inventory.available = 1;

      fwrite(&inventory, sizeof(struct Record), sizeof(struct Record), fp);

printf ("\nI wrote the record. %d   key=%d, name=%s", inventory.available, inventory.key, inventory.itemName);

      fclose (fp);

   }
   else
      while (inventory.available == 1)
      {

         inventory.key = inventory.key + 1;

         fread (&inventory.available, sizeof(int), 1, fp);

         if (inventory.available == 0)

         {

            fseek(fp, inventory.key * sizeof(struct Record), SEEK_SET);

            inventory.available = 1;

            fwrite (&inventory, sizeof(struct Record), sizeof(struct Record), fp);

printf ("\nResolved collision and wrote the file.");

            fclose (fp);         

         } // end if

      } // end while

return;
} 

void modRecord ()
{
   //local variables
   FILE* fp;
   struct Record inventory;

   printf ("\nPlease enter the Item ID: ");
   scanf ("%d", &inventory.itemID);

   fp = fopen("invent.txt", "rb+");

   inventory.key = inventory.itemID % 23;  

   fseek(fp, inventory.key * sizeof(struct Record), SEEK_SET);

   fread (&inventory.available, sizeof(int), 1, fp);

   if (inventory.available == 0)

      printf ("\nNo Record Found.");

   else if (inventory.available == 1)
   {

      fseek(fp, inventory.key * sizeof(struct Record), SEEK_SET);

      printf("\nPlease enter the Item Name (Limit of 15 Characters): ");
      scanf("%s", inventory.itemName);

      printf("\nPlease enter the Item Description (Limit of 30 Characters): ");
      scanf("%s", inventory.itemDescription);

      printf("\nPlease enter the Item Unit Price: $");
      scanf("%lf", &inventory.itemUnitPrice);

      printf("\nPlease enter the number of Items on Hand: ");
      scanf ("%d", &inventory.itemOnHand);

      inventory.value = inventory.itemUnitPrice * inventory.itemOnHand;  

      fp = fopen("invent.txt", "rb+");

      fseek(fp, inventory.key * sizeof(struct Record), SEEK_SET);

      fread (&inventory.available, sizeof(int), 1, fp);

      fwrite(&inventory, sizeof(struct Record), sizeof(struct Record), fp);

      fclose (fp);

   } // end if

return;

}

void dispOneRecord ()
{
   // local variables
   FILE* fp;
   struct Record inventory;   

   printf ("\nPlease enter the Item ID: ");
   scanf ("%d", &inventory.itemID);

   fp = fopen("invent.txt", "rb+");

   inventory.key = inventory.itemID % 23;  

printf ("\n%d", inventory.key);

   fseek(fp, inventory.key * sizeof(struct Record), SEEK_SET);

   fread (&inventory, sizeof(int), 1, fp);

printf ("\n%d, %s", inventory.available, inventory.itemName);

   if (inventory.available == 0)

      printf ("\nNo Record Found.");

   else if (inventory.available == 1)
   { 
      fseek(fp, inventory.key * sizeof(struct Record), SEEK_SET);
      fread (&inventory, sizeof(struct Record), sizeof(struct Record), fp);

printf ("\nI have found the record.");
      printf ("\nItem ID:                %-d\n", inventory.itemID);
      printf ("\nItem Name:              %-s\n", inventory.itemName);
      printf ("\nItem Description:       %-s\n", inventory.itemDescription);
      printf ("\nItem Unit Price:        $%-0.2lf\n", inventory.itemUnitPrice);
      printf ("\nNumber of Item on Hand: %-d\n\n\n", inventory.itemOnHand);
      printf ("\nTotal Value of Items on Hand: $%-0.2lf\n", inventory.value = inventory.itemUnitPrice * inventory.itemOnHand);
   } // end if
return;
}

void dispAllRecords ()
{

   // local variables
   FILE* fp;
   struct Record inventory;
   double tValue = 0;

   printf("\n ***********************************************************\n");    
   printf("\n                  Inventory Management System               \n");
   printf("\n ***********************************************************\n"); 

   inventory.key = 0;

   while (inventory.key < 23)
   {

      fseek (fp, inventory.key * sizeof(struct Record), SEEK_SET);

      fread (&inventory, sizeof(int), 1, fp);

      if (inventory.available == 0)
         inventory.key = inventory.key + 1;

      else 
      {

         fseek(fp, inventory.key * sizeof(struct Record), SEEK_SET);

         fread (&inventory, sizeof(struct Record), sizeof(struct Record), fp);

         printf ("\nItem ID:                %-d\n", inventory.itemID);
         printf ("\nItem Name:              %-s\n", inventory.itemName);
         printf ("\nItem Description:       %-s\n", inventory.itemDescription);
         printf ("\nItem Unit Price:        $%-0.2lf\n", inventory.itemUnitPrice);
         printf ("\nNumber of Item on Hand: %-d\n\n\n", inventory.itemOnHand);
         printf ("\nTotal Value of Items on Hand: $%-0.2lf\n", inventory.value = inventory.itemUnitPrice * inventory.itemOnHand);

         tValue = tValue + inventory.value;

         inventory.key = inventory.key + 1;
      } // end if
   } // end while

   printf ("\nThe total value of the inventory is: $%lf", tValue);

return;
} 

允许用户将数据输入到随机访问文件中 修改该记录 查找并显示一条记录 查找并显示所有记录并显示库存的总价值

0 个答案:

没有答案