hi I'm doing a banking project using binary files I need to write into a binary file and read and calculate the amounts within it. I open a file by asking the user for their name and searching and a file. I am having trouble reading the integers into a file and calculating.Here is my code so far.I'm also seem to have trouble with a file 'Index'(binary) which stores the users info but when I'm reading the file into a struct only one person is found whilst searching which was the first person entered in the file.Can anyone help me Please?!?!?

[LIST=1]
[*]#include   <stdio.h>
[*]#include   <string.h>
[*]#include   <stdlib.h>


[*]#define MAX 100


[*]int menu(void);
[*]int display(int i);
[*]void customer_search(void);
[*]void AccNo_search(void);
[*]void createFile(char *);
[*]void openFile(char *);
[*]void enter(void);
[*]void save(void);
[*]void load(void);

[*]struct catalog
[*]{
[*]       char name[80];
[*]       char AccNo[6];
[*]       char address[80];
[*]       unsigned date;
[*]       unsigned char month;
[*]      
[*]}*cat[MAX];

[*]struct bank
[*]{
[*]        char BankName[20];
[*]        unsigned LastAccNum;
[*]        unsigned sortCode;
[*]        
[*]}*bank[MAX];

[*]struct Account
[*]{
[*]   int amounts[100];    
[*]       
[*]}*acc[MAX];

[*]int top = 0;


[*]int main()
[*]{
[*]              int choice;
[*]              
[*]              load();
[*]              system("CLS");
[*]              do
[*]                {
[*]                         choice = menu();
[*]                         
[*]                         switch(choice)
[*]                               { 
[*]                                       case 1: enter();
[*]                                            break;
[*]                                       
[*]                                       case 2: customer_search();
[*]                                            break;
[*]                                            
[*]                                       case 3: AccNo_search();
[*]                                            break;
[*]                                            
[*]                                       case 4: save();
[*]                               }
[*]                } while(choice !=5);


[*]system("PAUSE");
[*]       return 0;

[*]}   
[*]            
[*]            
[*]            /* Return a menu selection*/
[*]            
[*]int menu(void)
[*]{
[*]    int i;
[*]    char str[80];
[*]    
[*]         printf("\t\n\aHallinan Savings Inc.\n\n");             
[*]         printf("1->Add Customer\n");
[*]         printf("2->Search by Name\n");
[*]         printf("3->Search by Account Number\n");
[*]         printf("4->Save added Customers\n");
[*]         printf("5->Quit\n");     
[*]              
[*]           do
[*]                {
[*]                                 printf("Make your selection:  ");
[*]                                 gets(str);
[*]                                 i = atoi(str);
[*]                                 printf("\n");                        
[*]                } while(i < 1 || i > 5);   
[*]                
[*]           return i;
[*]}                    
[*]                                      
[*]    /*Enter customer into file */
[*]    
[*]    
[*]void enter()
[*]{
[*]     int i;
[*]     char temp[80];
[*]     
[*]     
[*]     for (i = top; i<MAX ; i++)
[*]            {
[*]                                    
[*]                   cat[i] = malloc(sizeof(struct catalog));
[*]                   
[*]                          if(!cat[i])
[*]                                 {
[*]                                                 printf("Out Of Memory\n");
[*]                                                 return;
[*]                                 }
[*]                  
[*]                 printf("Enter customer name (ENTER to Quit):");
[*]                 gets(cat[i] -> name);                
[*]                 
[*]                             if(!*cat[i] -> name)
[*]                                         break;
[*]                 
[*]                 createFile(cat[i] -> name);
[*]                 
[*]                 
[*]                 printf("\nEnter AccNo:  ");
[*]                 gets(cat[i] -> AccNo);                
[*]                 
[*]                 printf("\nEnter address:  ");
[*]                 gets(cat[i] -> address);                
[*]                 
[*]                 printf("\nEnter year: ");
[*]                 gets(temp);                
[*]                 cat[i] -> date = (unsigned) atoi(temp);
[*]                 
[*]                 printf("\nEnter month: ");
[*]                 gets(temp);                
[*]                 cat[i] -> month = (unsigned) atoi(temp);
[*]                 
[*]                 
[*]                
[*]                }
[*]             
[*]     top =i ;
[*]                
[*]}
[*]    
[*]/* Search for customer */

[*]void customer_search()
[*]{
[*]     char name[80];
[*]     char ans;
[*]     int i , found;
[*]     
[*]         printf("Name:  ");
[*]         gets(name);
[*]         
[*]         found = 0;
[*]         
[*]               for (i=0; i<top; i++)
[*]                   {
[*]                         if (!strcmp(name,cat[i]->name))
[*]                                        {
[*]                                                 display(i);
[*]                                                 found = 1;
[*]                                                 printf("\n");
[*]                                               
[*]                                               
[*]                                               printf("\n\nWould you like to Open this customer file?(Y/N)");
[*]                                                   scanf("%c",&ans);
[*]                                                
[*]                                                 if(ans == 'y' || ans =='Y')
[*]                                                    {
[*]                                                        i-1;
[*]                                                       openFile(cat[i]->name);
[*]                                                     }
[*]                                        }
[*]                   }
[*]             
[*]             
[*]             
[*]                 
[*]             
[*]                  
[*]             if(!found)
[*]                   printf("Not Found\n");

[*]}



[*]void AccNo_search()
[*]{
[*]     char AccNo[80],ans;
[*]     int i , found;
[*]     
[*]         printf("AccNo:  ");
[*]         gets(AccNo);
[*]         
[*]         found = 0;
[*]         
[*]               for (i=0; i<top; i++)
[*]                   {
[*]                         if (!strcmp(AccNo,cat[i] -> AccNo))
[*]                                        {
[*]                                                 display(i);
[*]                                                 found = 1;
[*]                                                 
[*]                                                 printf("\n\nWould you like to Open this customer file?(Y/N)");
[*]                                                   scanf("%c",&ans);
[*]                                                
[*]                                                 if(ans == 'y' || ans =='Y')
[*]                                                    {
[*]                                                        i-1;
[*]                                                       openFile(cat[i]->name);
[*]                                                     }
[*]                                                     
[*]                                                     
[*]                                                 printf("\n");
[*]                                        }
[*]                   }
[*]                   
[*]             if(!found)
[*]                   printf("Not Found\n");
[*]}


[*]                       
[*]             /*Display entries*/    
[*]             
[*]int display(int i)
[*]{
[*]                  system("CLS");
[*]                  printf("%s\n",cat[i]->AccNo);
[*]                  printf("Account Holder Name: %s\n",cat[i]->name); 
[*]                  printf("address: %s\n",cat[i]->address);
[*]                  printf("Year and Month: %u %u ",cat[i]->date,cat[i]->month);
[*]}       
[*]    
[*]                        /*load file*/
[*]                        
[*]void load(void)
[*]{
[*]     FILE *fp;
[*]     FILE *fp2;
[*]     int i;
[*]     
[*]         if((fp =fopen("index","rb"))==NULL)
[*]                {
[*]                                              printf("File does not exist on disk");
[*]                                              return;
[*]                }
[*]                
[*]                
[*]         if(fread(&top,sizeof top,1,fp) !=1)
[*]                              {
[*]                                        printf("Error Reading count");
[*]                                        
[*]                              }
[*]                              
[*]         for (i=0 ; i < top ; i++)
[*]                  {
[*]                        cat[i] = malloc(sizeof(struct catalog));
[*]                               
[*]                               if (!cat[i])
[*]                                  {
[*]                                           printf("Out of Memory->\n");
[*]                                           top = i-1;
[*]                                           break;
[*]                                  }
[*]                                  
[*]                               if(fread(cat[i], sizeof(struct catalog),1,fp) !=1)
[*]                                                {
[*]                                                              printf("Error reading customer data->\n");
[*]                                                              
[*]                                                }
[*]                  }
[*]                  
[*]                  
[*]      if((fp2 =fopen("bank","rb"))== NULL )
[*]         {
[*]                    printf("File does not exist on disk");
[*]                      return;
[*]         }
[*]                
[*]                
[*]         if(fread(&top,sizeof top,1,fp2) !=1)
[*]                              {
[*]                                        printf("Error Reading count");
[*]                                      //  exit(1);
[*]                              }
[*]     
[*]     for (i=0 ; i < top ; i++)
[*]                  {
[*]                        bank[i] = malloc(sizeof(struct bank));
[*]                               
[*]                               if (!bank[i])
[*]                                  {
[*]                                           printf("Out of Memory->\n");
[*]                                           top = i-1;
[*]                                           break;
[*]                                  }
[*]                                  
[*]                               if(fread(bank[i], sizeof(struct bank),1,fp2) !=1)
[*]                                                {
[*]                                                              printf("Error reading catalog data->\n");
[*]                                        //                      exit(1);
[*]                                                }
[*]                  }
[*]     fclose(fp2);
[*]     fclose(fp);
[*]}


[*]/* Save the catalog file */

[*]void save()
[*]{
[*]    FILE *fp;
[*]     int i;
[*]     
[*]           if((fp =fopen("index","w+"))==NULL)
[*]                       {
[*]                                              printf("Cant open file->\n");
[*]                                              exit(1);
[*]                       }
[*]           if(fwrite(&top,sizeof top,1,fp) !=1)
[*]                               {
[*]                                        printf("Error Writting count.\n");
[*]                                        exit(1);
[*]                               }
[*]         
[*]         for(i=0 ; i < top ; i++)
[*]                 {
[*]                       if(fwrite(cat[i], sizeof(struct catalog), 1, fp) != 1)
[*]                                         printf("Error writing count.\n");
[*]                                        
[*]                 }
[*]                 
[*]         fclose(fp);
[*]               
[*]               
[*]}
[*]     
[*]void createFile(char *filename)
[*]{
[*]     
[*]         FILE *fp;
[*]         int i;
[*]     
[*]           if((fp =fopen(filename,"wb+"))==NULL)
[*]                       {
[*]                                              printf("\nCant open file->\n");
[*]                                              exit(1);
[*]                       }
[*]           if(fwrite(&top,sizeof top,1,fp) !=1)
[*]                               {
[*]                                        printf("\nError Writting count.\n");
[*]                                     
[*]                               }
[*]         
[*]         for(i=0 ; i < top ; i++)
[*]                 {
[*]                       if(fwrite(cat[i], sizeof(struct catalog), 1, fp) != 1)
[*]                                         printf("\nError writing count.\n");
[*]                                        
[*]                 }
[*]                 
[*]         fclose(fp);
[*]     
[*]     
[*]}                    


[*]void openFile(char *file)
[*]{

[*]     FILE *fp;
[*]     int i;
[*] printf("%s OPENED!!",file);    
[*]     
[*]             
[*]     
[*] if((fp =fopen(file,"rb"))==NULL)
[*]                {
[*]                                              printf("\nFile does not exist on disk");
[*]                                              return;
[*]                }
[*]                
[*]                
[*]         if(fread(&top,sizeof top,1,fp) !=1)
[*]                              {
[*]                                        printf("\nError Reading count");
[*]                                        
[*]                              }
[*]                              
[*]         for (i=0 ; i < top ; i++)
[*]                  {
[*]                        acc[i] = malloc(sizeof(struct Account));
[*]                               
[*]                               if (!acc[i])
[*]                                  {
[*]                                           printf("\nOut of Memory->\n");
[*]                                           top = i-1;
[*]                                           break;
[*]                                  }
[*]                                  
[*]                               if(fread(acc[i], sizeof(struct Account),1,fp) !=1)
[*]                                                {
[*]                                                              printf("\nError reading customer data->\n");
[*]                                                              
[*]                                                }
[*]                  }
[*]                  
[*]     
[*]}
[/LIST]

yeah, you cant post 300 lines of code and expect people to sift through it. even if it didnt wrap across line breaks.

try parse out your specific questions, and make a simple example of what your main (or first) problem is. then work from there.

This article has been dead for over six months. Start a new discussion instead.