Solved

File Processing

Posted on 2004-03-25
4
250 Views
Last Modified: 2010-04-15
EZPAY Inc. is an online financial entity, which allows its clients to transfer moneys from their personal accounts to the accounts of its member utilities companies. These member companies include:
1.      ElectriCo - The Electricity Company.
2.      TelCO - The Telephone Company
3.      GasCo - The Gas Company
4.      CTV - The Cable TV Company.
On registration, clients are provided with an account number and temporary personal identification number (PIN), which the client must change the first time the record is accessed.
 
At this testing stage, you are required to design a database system for EZPAY Inc. using C/C++, which will achieve the following:

1.      Store the client’s personal information. Such information must include an account number, PIN, first name, surname, street address, district, parish, telephone number, utilities subscribed, and cash balance. The file “CLIENTS.DOC”, provides a sample of what the input data should look like.
2.      A separate sequential file containing the client’s name and account number (“INFO.DAT”) is to be maintained. For searches in the database, this file is to be used to get the client’s account number when given the first name, surname or both in order to locate the client’s random access record.
3.      A record can then be accessed ONLY if the correct PIN is provided.
4.      The first time a record is accessed, the user is to be prompted to change the temporary PIN (9999) to some other positive 4-digit number. This new PIN should replace the temporary PIN in the record. 9999 should always be treated as a temporary PIN.
5.      Keep a record of all financial transactions.
6.      Use the account number as the record key and limit the range to 1000 - 5999.
======================================================================
This is my assignment and I'm having a bit of a problem with part 2. Below is how far I've reached and it works.


#include <stdio.h>
#include <conio.h>

/*clientData structure definition*/
struct clientData {
      int accountnum;         /*account number*/
   int pin;                /*pin number*/
   char firstname[15];     /*first name*/
   char lastname[15];      /*last name*/
   char streetadd[20];     /*street address*/
   char district[20];      /*district*/
   char parish[15];        /*parish*/
      char telnum[8];             /*telephone number*/
      char electric[1];       /*Electric Company*/
      char telephone[1];      /*Telephone Company*/
      char gas[1];            /*Gas Company*/
      char cable[1];          /*Cable TV Company*/
      float cashbal;          /*cash balance*/
};  /*end structure definition*/

/*Function prototypes*/
int menu(void);
void newrecord(FILE *fPtr);
void delrecord(FILE *fPtr);


int main()
{
      FILE *cfPtr;          /*clients.dat file pointer*/
      FILE *ifPtr;          /*info.dat file pointer*/
      int choice;

      /*fopen opens the files*/
      cfPtr = fopen("clients.dat","rb+");
      //ifPtr = fopen("info.dat","rb+");

      /*if files cannot be opened, then exits*/
      if (cfPtr == NULL) //&& (ifPtr == NULL))
      {
            printf("File cannot be opened\n");
      }
      else
      {

            while (( choice = menu() ) != 8)
            {
                  switch(choice)
                  {
                        /*add a new record to database*/
                        case 1:
                              newrecord(cfPtr);

                              break;
                        /*delete existing record*/
                        case 2:
                              delrecord(cfPtr);
                              break;

                        /*case 3:
                              break;

                        case 4:
                              break;

                        case 5:
                              break;

                        case 6:
                              break;

                        case 7:
                              break;
                  */
                  default:
                                    printf("\nInvalid selection\n");
                                    printf("Please try again\n\n");
                                    menu();
                                    break;
            }
            /*printf("\n\nPlease enter your selection from menu: ");
            scanf("%d",&selection);*/
      }
      printf("\nThis is the end, bye!\n\n");
      return 0;
} }


/* Menu Function */
int menu()
{
      int selection;                        /* variable to store user's selection*/

      /*display available options*/
      printf("######################################################################\n");
      printf("#                                                                    #\n");
      printf("#                           WELCOME                                  #\n");
      printf("#                              to                                    #\n");
      printf("#                          EZPAY Inc.                                #\n");
      printf("#                         ============                               #\n");
      printf("#                                                                    #\n");
      printf("#   |1| Enter New Record(s)       |5| Display Particular Account     #\n");
      printf("#   |=|                           |=|                                #\n");
      printf("#   |2| Delete Record(s)          |6| Display Summary of Accounts    #\n");
      printf("#   |=|                           |=|                                #\n");
      printf("#   |3| Authorize Payments        |7| Display Financial Summary      #\n");
      printf("#   |=|                           |=|                                #\n");
      printf("#   |4| Add Cash to Account       |8| Exit                           #\n");
      printf("#                                                                    #\n");
      printf("#                                                                    #\n");
      printf("######################################################################\n");

      printf("\n\nPlease enter your selection from menu: ");
      scanf("%d",&selection);                              /*read in selection from user*/

      return selection;

}  /*function menu*/


/*New Record Function*/
void newrecord(FILE *fPtr)
{
      /*create clientData with default information*/
      struct clientData client = {0,0," "," "," "," "," "," "," "," "," "," ",0.00};

      //struct clientData info = {0," "," "}
      int acctnum;

      /*obtain number of account to create*/
      printf("Enter new account number (1000 - 5999): ");
      scanf("%d",&acctnum);

      /*move file pointer to correct record in file*/
      fseek(fPtr, (acctnum - 1) * sizeof(struct clientData),SEEK_SET);

      /*read record from file*/
      fread(&client, sizeof(struct clientData), 1,fPtr);

      /*display error if record does not exist*/
      if (client.accountnum !=0)
      {
            printf("Account #%d already contains information.\n",client.accountnum);
      }
      else    /*create record*/
      {
            /*user enters PIN,firstname,lastname,street address,district,parish,
              telephone number, yes to utilies subscribed and no to those not subscribed*/
            printf("Enter temporary PIN: \n");
            scanf("%d",&client.pin);
            printf("Enter Firstname: \n");
            scanf("%s",&client.firstname);
            printf("Enter Lastname: \n");
            scanf("%s",&client.lastname);
            printf("Enter Street Address: \n");
            scanf("%s",&client.streetadd);
            fflush(stdin);
            printf("Enter District: \n");
            scanf("%s",&client.district);
            fflush(stdin);
            printf("Enter Parish: \n");
            scanf("%s",&client.parish);
            fflush(stdin);
            printf("Enter Telephone Number: \n");
            scanf("%s",&client.telnum);
            printf("Enter Y or N for Electic Company: \n");
            scanf("%s",&client.electric);
            printf("Enter Y or N for Telephone Company: \n");
            scanf("%s",&client.telephone);
            printf("Enter Y or N for Gas Company: \n");
            scanf("%s",&client.gas);
            printf("Enter Y or N for Cable TV Company: \n");
            scanf("%s",&client.cable);
            printf("Enter cash balance on account: \n");
            scanf("%f",&client.cashbal);
            clrscr();
            client.accountnum = acctnum;

            /*move file pointer to correct record in file*/
            fseek(fPtr, (acctnum - 1 ) * sizeof(struct clientData),SEEK_SET);

            /*insert record in file*/
            fwrite(&client, sizeof(struct clientData), 1,fPtr);
      }
}  /*end function newrecord*/

In this program I'm suppose to have two files. The second one - info.dat is only suppose to have in it the account number, firstname and lastname of the client while the first one - clients.dat has ALL the information.  

Question
How do I copy those 3 fields with information from clients.dat without having to read it in again in my newrecord function i.e. what's the code to copy file contents from one file to another without reading the info twice?

0
Comment
Question by:HuntressDi
4 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 10681378
When you are creating a new record, you obtain the clients name and account number, and save it in the client structure. You write out an entry in CLIENT.DAT that contains all the information in client.

I would expect that, at that time, you also make an entry in INFO.DAT using the name and account fields you already have in the client structure. You could write out the relevant fields one by one.

Does that answer the question?
0
 
LVL 45

Accepted Solution

by:
Kdo earned 125 total points
ID: 10681717
Hi HuntressDi,

You've got two options here.  1)  Read the file INFO.DAT every time that you need to get a record; or 2)  Read the file into a buffer once and then just check the entries in the buffer.

I don't see where you're really using INFO.DAT.  To test your program, you can just read the file every time that you need to find a record.  I'll provide you with a function to read through the INFO.DAT file, you'll have to write the code to update it.


typedef struct
{
  char FirstName[20+1];
  char LastName[20+1];
  char AccountNumber[12+1];
  long Offset;
} info_t;


int FindRecord (info_t *InfoRecord, char *FirstName, char *LastName)
{
  FILE *info;

  info = fopen ("info.dat","r");
  if (info == NULL)
    return (1);          /*  open failed  */

  while (fread (InfoRecord, 1, sizeof (info_t), info)
  {
    if (stricmp (FirstName, InfoRecord->FirstName == 0 && LastName, InfoRecord->LastName == 0))
    {
      fclose (info);
      return 0;
    }
  }
  fclose (info);
  return (2);           /*  record not found  */
}


Good Luck,
Kent
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10684137
Store info. in your info.dat file in a similar manner as u do for ur clients.dat file.
For every record inserted,check whether the record for that client exists in the info.dat file.
If it does not exist,write it into the info.dat file otherwise skip the write to info.dat
0
 

Author Comment

by:HuntressDi
ID: 10691187
Thank you for your help everyone that answered. I try your suggesstions. Thanks again and God Bless! :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now