[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.2

c programming project

Asked by HPG in C Programming Language

I've written this program, accessing a binary file and then comparing the data and writing to a new file, however the program on msdos does not work. I can't find the errors. Please Help.

/**************************************************************************/
/*This program is written for Zenith Paints Co. to provide a solution        */
/*The new sorted file from program two is used to update a line sequential*/
/*master file and this program will create a new master file.                  */
/*reference is made to an indexed sequential stock master file for price  */
/*information. A header is used to access this information called 'isam.h'*/
/*Any invalid transactions are then printed onto an error report.              */
/**************************************************************************/


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<time.h>
#include<math.h>
#include "a:isam.h"      /*a header file storing stock data*/
#include"a:printer.h"

#define SIZE sizeof(struct master)
#define ISSUE struct I_R
#define DELREC struct deletion
#define CREREC struct create
#define SIZE_U sizeof(union valid)
#define PRINT "LPT1"
#define TRUE 1
#define FALSE 0
#define LASTCODE "ZZZZZ" /*ensures that if one file finishes first
the other one will continue to be processed*/


/*declare structures and union to hold the customer details*/
/*These structures are determined by the file specifications*/

struct stock                        /*structure held on header file*/
{
      char cust_code[6],
            cust_name[21],
            cust_add[61];
      float cust_bal;
      long credit_limit;
      int year,
            month,
            day;
} ;
struct stock new_s;

struct master                  /*structure held on master file*/
{
      char cust_code[6],
            cust_name[21],
            cust_add[61];
      float cust_bal;
      long credit_limit;
      int year,
            month,
            day;
};

struct I_R                        /*The structure for issue/receipt record*/
{
            char rec_type,
                  cust_code[6],
                  part_num[7],
                  iss_rec_qty[5];
            };

      struct deletion            /*The structure for delete record*/
            {
            char rec_type,
                  cust_code[6];
            };
            
      struct create            /*The structure for create record*/
            {
            char rec_type,
                  cust_code[6],
                  cust_name[21],
                  cust_add[61],
                  cust_bal[10],
                  cust_limit[8];
            };

union valid                        /*Union to hold all three structures*/
{
      ISSUE i;
      DELREC d;
      CREREC c;
};

/*********************************************************************/
/*Global declarations.                                                                         */
/*********************************************************************/

struct tm *todays_date;
time_t calendar_time;

/*declare function prototypes and theire names indicate*/
/*their purpose*/
void store_new(FILE *fp, struct master temp);
void print_headings(FILE *out_prn);
void error(FILE *out_prn, union valid u, int i);

/*********************************************************************/
/*      coding for solution                                                                         */
/*********************************************************************/

void main()
{

FILE *cp, *sort, *out_prn, *new_fp; /*file pointers for the four file*/
struct master temp;                              /*structure variable*/
union valid u;                                    /*union to hold the structures       */
int compare, records=0;                        /*compare variable to hold comaprison of*/
                                                      /*two structure*/                  
char ch;
char cust_code[4];                              /*to hole the customer code*/
float  total=0;
RECORD_DATA *stock_rec;      

calendar_time = time(NULL);
todays_date = localtime(&calendar_time);

                  /*to access the header file stock details*/


      if((out_prn=fopen(PRINT,"wt"))==NULL)
      {
            fprintf(stderr,"Error cannot open\n");/*if file does not open then exit*/
            exit(1);
      }

      if((cp=fopen("A:Custmast.dat","rb"))==NULL)
      {
            fprintf(out_prn,"Error cannot open\n");/*if file does not open then exit*/
            exit(1);
      }

if((sort=fopen("a:sort.dat","rb"))==NULL)
      {
            fprintf(out_prn,"Error cannot open sort\n");/*if file does not open then exit*/
            exit(1);
      }
      

if((new_fp=fopen("A:new.dat","wb+"))==NULL)
      {
            fprintf(stderr, "Error cannot open\n");/*if file does not open then exit*/
            exit(1);
      }


      print_headings(out_prn);                              /*declare fundion to print headings*/
            

            if(fread(&u,SIZE_U, 1, sort)!=1)
            {
                  fprintf(stderr, "Unable to read sort file\n");/*if file does not open then exit*/
                  exit(1);
            }
                        /*read first record from sorted file*/
            if(fread(&temp, SIZE, 1, cp)!=1)                        /*read first record from master file*/
            {
                  fprintf(stderr, "unable to read master file\n");/*if file does not open then exit*/
                  exit(1);
            }
                  /*copy 'ZZZZZ' to customer code to compare*/
                  /*if last record*/
      strcpy(u.i.cust_code, LASTCODE);

      while(strcmp(u.i.cust_code, LASTCODE)!=0 ||
       strcmp(temp.cust_code, LASTCODE)!=0)

      {
            /*store value in compare*/
             compare=strcmp(u.i.cust_code, temp.cust_code);

            /*there are a few possibilities here as */
             /*if the answer is yes than a check is      */
             /*done for the record type before a new */
             /*record is read*/

       if(compare<0)
       {
            
             ch=u.i.rec_type;
             ch=toupper(ch);

                  if(ch=='C')
                        store_new(new_fp, temp);
            
                  else
                        if(ch=='I'||ch=='R'||ch=='D')
                  error(out_prn,u, 1);

            if(fread(u.i.cust_code, SIZE_U, 1, sort)!=1)
            {
                  /*if eof occurs store lastcode in cust_code*/
            strcpy(u.i.cust_code, LASTCODE);
            } /*end if*/

       }/*end if*/

       if(compare>0)
       {
             store_new(new_fp, temp);
             if(fread(&temp, SIZE_U, 1, cp)!=1)
            {
      
            /*if eof occurs store lastcode in cust_code*/

            strcpy(u.i.cust_code, LASTCODE);
            } /*end if*/
             
       } /*end if*/


       if(compare==0)
       {
            
            ch=u.i.rec_type;
             ch=toupper(ch);

             switch(ch)
             {
                   case 'I':
                   case 'R':
                         
                               if((stock_rec=search_file(cust_code))!=NULL)
                              
                               {                                                              
                                     total=atoi(u.i.iss_rec_qty) * stock_rec->price;
                               }       
                                     if(ch=='I')
                                           temp.cust_bal+=total;
                                     else
                                           if(ch=='R')
                                          temp.cust_bal-=total;
                              
                                          else
                                     fprintf(out_prn,"Error in I\n");

                        
                        if(fread(&u, SIZE_U, 1, sort)!=1)
                              {
      
            /*if eof occurs store lastcode in cust_code*/

                              strcpy(u.i.cust_code, LASTCODE);
                              } /*end if*/
                                    store_new(new_fp, temp);

                               break;

                   case 'D':
                                       if(temp.cust_bal!=0)
                                                fprintf(out_prn,"Error in D\n");
                        if(fread(&u, SIZE_U, 1, sort)!=1)
                              {
      
            /*if eof occurs store lastcode in cust_code*/

                              strcpy(u.i.cust_code, LASTCODE);
                              } /*end if*/
                        else
                                       //eof1=readfile(sort);
                               if(fread(&u, SIZE_U, 1, sort)!=1)
                                    {
      
            /*if eof occurs store lastcode in cust_code*/

                                          strcpy(u.i.cust_code, LASTCODE);
                                    } /*end if*/
                                          if( fread(&temp, SIZE, 1, cp)!=1)
                                          {
      
            /*if eof occurs store lastcode in cust_code*/

                                                strcpy(temp.cust_code, LASTCODE);
                                          } /*end if*/
                                            

                         break;
                   case 'C': error(out_prn,u, 2);
                        if(fread(&u, SIZE_U, 1, sort)!=1)
                              {
      
            /*if eof occurs store lastcode in cust_code*/

                              strcpy(u.i.cust_code, LASTCODE);
                              } /*end if*/
                         

                         break;

            
                   default: break;
                  }/*end switch*/

            } /*end if*/
records++;
fflush(stdin);

}/*end while till eof*/

            fclose(new_fp);
             fclose(out_prn);
             fclose(sort);
             fclose(cp);
             
} /*end main*/

/*********************************************************************/
/*store_new()                                                                               */
/*Parameter:      new_fp. A pointer to the new file                              */
/*                        temp.      structure in old master file                        */
/*                                                                                                      */
/*                        Return:            none.                                                       */
/*                                                                                                       */
/*  A function that will use live local time and date each time the */
/*old data is stored into the new structure and then written to the      */
/*new file                                                                                          */      
/*********************************************************************/


void store_new(FILE *new_fp, struct master temp)
{
      
      int loop=0;
      struct tm today;
      time_t time_in_secs;
      time_in_secs = time(NULL);
      today = *localtime(&time_in_secs);

      strcpy( new_s.cust_name,temp.cust_name);
      strcpy(new_s.cust_add,temp.cust_add );
      strcpy(new_s.cust_code, temp.cust_code );
      new_s.cust_bal=temp.cust_bal;
      new_s.credit_limit=temp.credit_limit;

      new_s.year=today.tm_year;
      new_s.month=today.tm_mon+1;
      new_s.day=today.tm_mday;


      if(fwrite(&new_s, sizeof(struct stock), 1, new_fp)!=1)
{
      printf("Error in writing to new file");
      exit(1);

} /*end if*/
loop++;
return;

} /*end function*/

/*********************************************************************/
/*error()                                                                                           */
/*Parameter:      i. An int to indicate the message required.                   */
/*                        out_prn.      pointer to the printer file                         */
/*                        u.            Pointer to the union.                                     */
/*      Return:            none.                                                                         */
/*                                                                                                       */
/*  A simple routine to display an appropriate error message.             */
/*********************************************************************/


void error(FILE *out_prn, union valid u, int i)
{

      char *e[]={"Error\nError in creation\nError in Delete"};

      fprintf(out_prn,"\t%s\t\t\t%s\n", u.i.cust_code, e[i]);

} /*end function*/

/*********************************************************************/
/*print_headings()                                                                               */
/*Parameter:      out_prn. A pointer to the printer file                         */
/*Return:            none.                                                                         */
/*                                                                                                       */
/*  A function to display the errors for the report.                         */
/*********************************************************************/


void print_headings(FILE *out_prn)
{
      static int page;

      page++;
      bold(out_prn,ON);
    condensed(out_prn, OFF);

      fprintf(out_prn,"\n\tTRANSACTIONS FOR ZENITH PAINTS CO%30.24sPage %3d\n\n"
         "\t\t\t",asctime(todays_date), page);

      fprintf(out_prn,"\tCust Code\t\t\tError Reason\n\n");
      bold(out_prn,OFF);

}/*end func*/


 
Loading Advertisement...
 
[+][-]12/22/02 11:58 AM, ID: 7621200Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/22/02 12:09 PM, ID: 7621234Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/22/02 12:27 PM, ID: 7621288Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/22/02 12:32 PM, ID: 7621313Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/24/02 04:39 AM, ID: 7628426Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/24/02 11:01 AM, ID: 7629553Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/26/02 09:30 PM, ID: 7634688Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/27/02 12:14 PM, ID: 7637247Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/27/02 02:07 PM, ID: 7637690Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: C Programming Language
Sign Up Now!
Solution Provided By: Exceter
Participating Experts: 1
Solution Grade: B
 
 
Loading Advertisement...
20091111-EE-VQP-89