Thread: File Manipulation

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    File Manipulation

    Hello there.

    The purpose of this program is to enter data about customers and create a word document based on this data.

    The problem rests in my file manipulation.

    I wish to be able to search, edit and delete. In order to do so I have read I require a binary file and have thus started utilizing them.

    However, upon trying to read my data, I repeatedly get errors. I have salvaged the code to a point where it no longer crashes etc for your viewing ease.

    Please do your best.

    Notes:

    I am not 100% it is WRITING properly.
    I am still in the process of learning C, please assist.


    Windows 7
    Latest Dev version.

    Please assist me, this has been frustrating me for almost 1 week. I don't usually like to bother people with things they will probably consider "basic" .

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    #include <ctype.h>
    
    
    #define MaxRecords 10
    #define MaxProducts 10
    
    #define TRUE 1
    #define FALSE 0
    
    enum{EMPTY,TAKEN};
    
    
    void MainMenu();
    void InputCustData();
    void InputItemData();
    void IntroductoryOutput();
    void FlashOutput();
    float CalcTotPrice(int,float);
    void ConfirmItemNum(int);
    void DisplayCustDataMEM();
    void DisplayAllRecords();
    
    
    int c=1;
    int *item_num_ptr;
    FILE *fp= NULL;                                 /* File pointer                           */
    char *filename = "records.txt";
    
    
    FILE *CDPtr;
    
    struct CustomerItems
    {
           char ProdID[20][MaxProducts];
           char Desc[50][MaxProducts];
           int Qty[MaxProducts];
           float Price[MaxProducts];
           float TotPrice[MaxProducts];
           
    };
    
    struct CustomerItems I_array[MaxRecords]={EMPTY};
    
    
    
    
    struct CustomerData
      {
             /*Members of the structure*/
             char fName[30];
             char lName[30];  
             char SalrepName[45];   
             char ShipDate[20];
             char DueDate[20];
             struct CustomerItems CustItems;               
            
      };
      
    struct CustomerData C_array[MaxRecords]={EMPTY};
    
    
    
    main()
    {
          CreateDatabase();
          IntroductoryOutput();
          MainMenu();
    }
    
    
    void IntroductoryOutput()
    {
          FlashOutput();              
          printf("\n\nThe functions of this program are as follows:\n\n\n ");
    	  printf("\t ______________________________________________________\n"); 
          printf("\t|- Creating invoices.                                  |\n"); 
          printf("\t|- Displaying the total Customer records present.      |\n"); 
          printf("\t|- Retrieving particular invoice files.                |\n"); 
          printf("\t|- Alter customer records.                             |\n");
    	  printf("\t|- Searching for a particular record.                  |\n");
          printf("\t|______________________________________________________|\n");
          getch();
    }
    
    void FlashOutput()
    {
          printf("+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+\n\n");
          printf("   0000  PPPP  EEEE   RR RRR         A      TTTTTT   IIIIII   0000  NNN   NN\n");
          sleep(200);
          printf("   0  0  P  P  EE     RR RRR        A A       TT       II     0  0  NNNN  NN\n");
          sleep(200);
          printf("   0  0  PPPP  EEEE   RRRRRR       AAAAA      TT       II     0  0  NN NN NN\n");
          sleep(200);
          printf("   0  0  PP    EE     RRR  RR     A     A     TT       II     0  0  NN  NNNN\n");
          sleep(200);
          printf("   0000  PP    EEEE   RRR  RRR   A       A    TT     IIIIII   0000  NN  NNNN\n\n");
          printf("+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+\n");  
          
    }            
                  
    void MainMenu()
    {
        char achoice; 
        do
        {
          system("cls");   
          printf("+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+\n\n");
          printf("      MM    MM   AAA   IIIII NN   NN       MM    MM EEEEEEE NN   NN UU   UU\n");
          sleep(200); 
          printf("      MMM  MMM  AAAAA   III  NNN  NN       MMM  MMM EE      NNN  NN UU   UU\n");
          sleep(200); 
          printf("      MM MM MM AA   AA  III  NN N NN       MM MM MM EEEEE   NN N NN UU   UU\n");
          sleep(200); 
          printf("      MM    MM AAAAAAA  III  NN  NNN       MM    MM EE      NN  NNN UU   UU\n");
          sleep(200); 
          printf("      MM    MM AA   AA IIIII NN   NN       MM    MM EEEEEEE NN   NN  UUUUU \n\n");
          sleep(200);
          printf("+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+\n");   
             
          
          printf("\n\n");    
          printf("\t ____________________________________________________________________\n");
          printf("\t|                                                                    |\n"); 
          printf("\t| - Enter '1' in order to enter customer data.                       |\n"); 
          printf("\t| - Enter '2' to display Customer Datasheet.                         |\n"); 
          printf("\t| - Enter '3' to retrieve the invoice file of a particular customer. |\n"); 
          printf("\t| - Enter '4' to delete a customer record.                           |\n");
    	  printf("\t| - Enter '5' to terminate the program.                              |\n");
          printf("\t|____________________________________________________________________|\n");         
          scanf("%c", &achoice);
    	  fflush(stdin);
    	  
    	
    	  switch(achoice)
    	  {
              case '1':system("cls");
                       WriteCustData();
              break;
    	      case '2':system("cls"); 
                        DisplayAllRecords();
              break;
    	      case '3':system("cls"); 
                       //find_numbers();;
              break;
    	      case '4':system("cls");
                       //delete_records();
              break;
    	      case '5': exit(0);
    	      break;
    	      default: printf("\n\nWrong choice entered.\n"); 
                       printf("Press any key to continue,please enter a number between 1 and 5!\n\n");
              break;        
          }
    	getch();
         
          
         
          
        }while(achoice != 5);
    
    }
    
    
    void InputCustData()
    {
                                               
                   
                   printf("\n\n");
                   printf("----------------------------------------------------\n");
                   printf("Please enter the following data for the customer:  \n");
                   printf("----------------------------------------------------\n\n");
                   printf("\n\nPlease enter the customer's first name:  ");
                   scanf("%s",C_array[c].fName);
                   fflush(stdin);
                   printf("\n\nPlease enter the customer's last name:  ");
                   scanf("%s",C_array[c].lName);
                   fflush(stdin);
                   printf("\n\nPlease enter the Sales Representative of customer:  "); 
                   fgets(C_array[c].SalrepName,30,stdin);
                   fflush(stdin);
                   printf("\n\nPlease enter the date the item was shipped/distributed:  ");
                   scanf("%s",C_array[c].ShipDate);
                   fflush(stdin);
                   printf("\n\nPlease enter the date the item is due for arrival:  ");
                   scanf("%s",C_array[c].DueDate);
                   fflush(stdin);       
                                
                   
                   printf("\n\nPress any key in order to proceed to entering the item data.\n");
                   getch();
                   
                   InputItemData();
    }          
    
    
    
    void InputItemData()
    {
         int itemnum;
         
         system("cls");
         printf("Please enter the number of items the customer has purchased:\n");
         scanf("%d",&itemnum);
         fflush(stdin);
         
         while(itemnum > MaxProducts)
         {
                    printf("\nThe maximum number of products allowed is: %d",MaxProducts);
                    printf("\nPlease enter a number less than %d.\n\n",MaxProducts);
                    scanf("%d",&itemnum);
         }
         
         item_num_ptr=&itemnum;
         ConfirmItemNum(itemnum);
         
         
         
         int i;
         
         for(i=1;i <=itemnum;i++)
         {
                   system("cls");
                   printf("\t\t\t\t Item %d\n\n",i);
                   printf("\n\nPlease enter the Product ID of the item:\n\n");
                   scanf("%s",I_array[c].ProdID[i]);
                   fflush(stdin);
                   printf("\n\nPlease enter the description of the item:\n\n");
                   fgets(I_array[c].Desc[i],100,stdin);
                   fflush(stdin);
                   printf("\n\nPlease enter the quantity of the item:\n\n");
                   scanf("%d",&I_array[c].Qty[i]);                               
                   fflush(stdin);
                   printf("\n\nPlease enter the unit price of the item:\n\n");
                   scanf("%f",&I_array[c].Price[i]);                               
                   fflush(stdin);               
         
                   I_array[c].TotPrice[i]=CalcTotPrice(I_array[c].Qty[i],I_array[c].Price[i]);
                   printf("\n\nThe total price of %s is: %f",I_array[c].ProdID[i],I_array[c].TotPrice[i]);
                   sleep(1500);   
         } 
         
         
         ConfirmCustData();
    }             
    
    void ConfirmItemNum(int itemnum)
    {
         char confirmI;
         
         printf("\n\nBegin entry process for %d products for customer %s %s  ?\n\n",itemnum,C_array[c].fName,C_array[c].lName);
         printf("Enter:\n");
         printf("Y = 'Yes'\nN = 'No'\n");
         printf("\n\nNote: Entering 'N' will result in the user being allowed re-entry of the data.\n");
         scanf("%c",&confirmI);
         
         
         
         switch(confirmI)
         {
                         case 'Y': 
                         break; 
                         case 'N': system("cls");
                                   ReEntry();
                                   system("cls");                    
                                   InputItemData();
                         break;
                         default: system("cls");
                                  printf("\nYou have entered neither Y nor N.");
                                  printf("\nPlease enter Y or N in order to progress.");
                                  ConfirmItemNum(itemnum);
         }                         
    }
     
    float CalcTotPrice(int IQty,float IPrice) 
    {
               float Totprice;
               
               Totprice= IQty * IPrice;
               return Totprice;
               
    }  
    
    ConfirmCustData()
    {    
                        
        printf("The data you have entered will be displayed below:\n\n\n:");
        DisplayCustDataMEM();
        
        char confirmall;
        
        printf("Is this the data you wish to be stored?\n\n");
        printf("Enter:\n");
        printf("Y = 'Yes'\nN = 'No'\n");
        printf("\n\nNote: Entering 'N' will result in the user being allowed re-entry of the data.\n");
        printf("\n\nNote: Entering 'N' will result in all data being entered once again.\n\n");
        scanf("%c",&confirmall); 
        
        
        switch(confirmall)
         {
                         case 'Y': WriteCustFile();
                         break; 
                         case 'N': system("cls");
                                   ReEntry();
                                   system("cls");                    
                                   
                                   
                         break;
                         default: system("cls");
                                  printf("\nYou have entered neither Y nor N.");
                                  printf("\nPlease enter Y or N in order to progress.");
                                  ConfirmCustData();
         }                         
    } 
    
    
                                 
    
    void DisplayCustDataMEM()
    {
          system("cls");
          printf("______________________________________________________________________________\n\n");         
          printf("\t\t\t     CUSTOMER INFORMATION\n\n");
          printf("______________________________________________________________________________\n");               
          printf("Customer First Name :%s\n\n",C_array[c].fName);
          printf("Customer Last Name: %s\n\n",C_array[c].lName);
          printf("Sale Representative: %s\n\n",C_array[c].SalrepName);
          printf("Ship Date: %s\n\n",C_array[c].ShipDate);
          printf("Arrival Date(Estimated)%s:\n\n",C_array[c].DueDate);
          printf("______________________________________________________________________________\n\n");
          
          
          int itemnum;
          itemnum=*item_num_ptr;
          
          int i;
          
          printf("\t\t\t       ITEMS PURCHASED\n\n");
          printf("______________________________________________________________________________\n");
          for(i=1;i <=itemnum;i++)
          {           
                   printf("\t\t\t\t   Item %d\n\n",i);
                   printf("Product ID :%s \n\n",I_array[c].ProdID[i]);
                   printf("Description :%s \n\n",I_array[c].Desc[i]);               
                   printf("Quantity :%d \n\n",I_array[c].Qty[i]);
                   printf("Unit Price :%f \n\n",I_array[c].Price[i]);
                   printf("Total Price(Quantity X Unit Price) :%f \n\n",I_array[c].TotPrice[i]);
                   printf("------------------------------------------------------------------------------\n");
          }
    }
                     
    
                    
         
    CreateDatabase()
    {
        int i;
        
        struct CustomerData blankCust={EMPTY};
        
        
        
        if((CDPtr=fopen("CustomerDatabase.doc","wb"))==NULL)
        {
           printf("The file could not be opened.\n");
           abort();
        }
        else
        {
           for(i=1;i<=MaxRecords;i++)
           {
              fwrite(&blankCust,sizeof(struct CustomerData),1,CDPtr);
           }
           
           fclose(CDPtr);
           
        } 
        fclose(CDPtr);
        
        return 0;
        
    } 
    
    
                
                   
                                                           
    ReEntry()
    {
           system("cls");
           
           printf("\n\n\n\n\n");      
             
           printf(" RRRRRR    EEEE                EEEE   NNN   NN   TTTTTT   RRRRRR   Y    Y   \n");
           printf(" RR  RR    EE     -----------  EE     NNNN  NN     TT     RR  RR    Y  Y      \n");
           printf(" RRRRRR    EEEE   -----------  EEEE   NN NN NN     TT     RRRRRR     YY       \n");
           printf(" RRR  RR   EE     -----------  EE     NN  NNNN     TT     RRR  RR    YY     \n");
           printf(" RRR  RRR  EEEE                EEEE   NN  NNNN     TT     RRR  RRR   YY     \n\n");
           sleep(1200);
    }
    
    WriteCustData()
    {
         struct CustomerData Cust={EMPTY};
         
         if((CDPtr=fopen("CustomerDatabase.doc","ab"))==NULL)
         {
            printf("The file could not be opened.\n");
            abort();
         }  
         else
         {
             InputCustData();
             
         } 
         
         fclose(CDPtr); 
         
         return 0;
    }   
    
    WriteCustFile()
       {
            int CheckCustFile;
            CDPtr = fopen("CustomerDatabase.doc", "wb+");
            if(CDPtr == NULL)
            {
               
               fprintf(stderr, "Error opening file. Returning to EngineerMenu\n");
               return;
            }
    
            CheckCustFile = fwrite(C_array, sizeof(struct CustomerData), MaxRecords, CDPtr);
            if(CheckCustFile != MaxRecords)
            {
               
               fprintf(stderr, "Error reading file. Returning to EngineerMenu\n");
               return;
            }
            fclose(CDPtr);
    }
    
    
    void DisplayAllRecords()
       {
            int CheckCustFile;
            CDPtr = fopen("CustomerDatabase.doc", "rb");
            if(CDPtr == NULL)
            {
               
               fprintf(stderr, "Error opening file.\n");
               return;
            }
    
            
            if(CheckCustFile != MaxRecords)
            {
               
               fprintf(stderr, "Error reading file. \n");
               return;
            }
            for(;;)
            {
                   fread(&C_array, sizeof(struct CustomerData), 1, CDPtr);
                   
                   if(feof(CDPtr))
                   {
                       break;
                   }
                                  
                   DisplayCustDataMEM();
            }       
            fclose(CDPtr);
    }

    Edit: Code isn't as clear as I'd like, is there any way to make it clearer for you guys? Please inform me.

    I don't mean for it to be...hard to read.

    Edit: Added C file.
    Last edited by Pr0AHP; 04-11-2010 at 08:52 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM