#include<conio.h>
#include<string.h>

//prototype declaration
int pnc1(FILE *fp);
int pnc2(FILE *fp);
int findstr(char *p[], char *name);
int sum_array(int a[][], int element);

//function to pick data and then convert bits to number
// for notes_disp , notes_req
int pnc1(FILE *fp)
{
    int tot=i=j=k=0;
    i=getw(fp);
    j=getw(fp);
    k=getw(fp);
    tot=(100 * i) + (10 * j) + k;
    return(tot);
}

//for notes_rejt
int pnc2(FILE *fp)
{
    int tot=i=j=0;
    i=getw(fp);
    j=getw(fp);
    tot=(10 * i) + j;
    fseek(fp,1,SEEK_CUR);
    return(tot);
}

//find a string and returns value
int findstr(char *p[], char *name)
{
    int t;
    for(t=0; p[t]; ++t) {
        if(!strcmp(p[t],name))
            return t;
    }
    return -1;// string not found
}

//calculates total of array elements i
int sum_array(int a[][], int element)
{
    int j, sum=0;
    for(j=0; j<=elements; j++) {
        sum=sum+a[][j];
    }
}

//main starts
void main()
{
    FILE *fp;
    int notes_disp[][], notes_req[][], notes_rejt[][];
    int flag=1;
    int i=0,m;
    int sum, sum2,j;
    int disp_sum[3], req_sum[3], rejt_sum[3];
    char c,temp1,temp2;
    char buffer[500];


    fp=fopen("CashDispense_Log_18_10_2011.txt","r"); // open file
    if (fp==NULL)
        perror ("The following error occurred : "); // if file not open, print error
    else {
        do {
            c=fgetc(fp); // takes a character
            if(c=='%') {
                if(flag==1) {
                    do {
                        temp1=fgetc(fp);
                        if(temp1=='A') {
                            sum=pnc1(fp);
                            notes_req[i][0]=sum;
                        }
                        if(temp1=='B') {
                            sum=pnc1(fp);
                            notes_req[i][1]=sum;
                        }
                        if(temp1=='C') {
                            sum=pnc1(fp);
                            notes_req[i][2]=sum;
                        }
                        if(temp1=='D') {
                            sum=pnc1(fp);
                            notes_req[i][3]=sum;
                        }
                        flag=0;
                    }
                    while(!fgetc(fp)==" ");
                }
                else { //flag==0
                    fseek(fp,4,SEEK_CUR);
                    do {
                        temp2=fgetc(fp);
                        if(temp2=='A') {
                            sum=pnc1(fp);
                            notes_disp[i][0]=sum;
                            sum2=pnc2(fp);
                            notes_rejt[i][0]=sum2;
                        }
                        if(temp2=='B') {
                            sum=pnc1(fp);
                            notes_disp[i][1]=sum;
                            sum2=pnc2(fp);
                            notes_rejt[i][1]=sum2;
                        }
                        if(temp2=='C') {
                            sum=pnc1(fp);
                            notes_disp[i][2]=sum;
                            sum2=pnc2(fp);
                            notes_rejt[i][2]=sum2;
                        }
                        if(temp2=='D') {
                            sum=pnc1(fp);
                            notes_disp[i][3]=sum;
                            sum2=pnc2(fp);
                            notes_rejt[i][3]=sum2;
                        }
                        flag=1;
                    }
                    while(!fgetc(fp)==" ");
                }
                i++;
            }
        }
        while(!feof(fp));

//test dispenser count
        fseek ( fp , 0L , SEEK_SET );// reset to beginning of file

//calculate total rejected till yet
        for(r=0; r<=3; r++) {
            rejt_sum[r]=sum_array(notes_rejt, i);
            disp_sum[r]=sum_array(notes_disp, i);
            req_sum[r]=sum_array(notes_req, i);
        }

        fseek(fp,0,SEEK_SET);

        do {
            fgets(buffer,500,fp);

            if(findstr(buffer,"Type 1 Rejected")!=1) {
                sscanf(buffer,"Type 1 Rejected %4d",&m);
                if(m>rejt_sum[0])
                    rejt_sum[0]=rejt_sum[0]+(m-rejt_sum[0]);
                else
                    rejt_sum[0]=rejt_sum[0]+m;
            }

            if(findstr(buffer,"Type 2 Rejected")!=1) {
                sscanf(buffer,"Type 2 Rejected %4d",&m);
                if(m>rejt_sum[1])
                    rejt_sum[1]=rejt_sum[1]+(m-rejt_sum[1]);
                else
                    rejt_sum[1]=rejt_sum[1]+m;
            }

            if(findstr(buffer,"Type 3 Rejected")!=1) {
                sscanf(buffer,"Type 3 Rejected %4d",&m);
                if(m>rejt_sum[2])
                    rejt_sum[2]=rejt_sum[2]+(m-rejt_sum[2]);
                else
                    rejt_sum[2]=rejt_sum[2]+m;
            }

            if(findstr(buffer,"Type 4 Rejected")!=1) {
                sscanf(buffer,"Type 4 Rejected %4d",&m);
                if(m>rejt_sum[3])
                    rejt_sum[3]=rejt_sum[3]+(m-rejt_sum[3]);
                else
                    rejt_sum[3]=rejt_sum[3]+m;
            }

        }
        while(!feof(fp));

//printing values

        printf("\n\n Number of times Dispenser called : %d",i);
        printf("\n\n Number of notes Requested\n \t TYPE_1 %d \t TYPE_2 %d \t TYPE_3 %d \t TYPE_4 %d",sum_req[0],sum_req[1],sum_req[2],sum_req[3]);
        printf("\n\n Number of notes Dispensed\n \t TYPE_1 %d \t TYPE_2 %d \t TYPE_3 %d \t TYPE_4 %d",sum_disp[0],sum_disp[1]),sum_disp[2];
        ,sum_disp[3]
        printf("\n\n Number of notes Rejected\n \t TYPE_1 %d \t TYPE_2 %d \t TYPE_3 %d \t TYPE_4 %d",sum_rejt[0],sum_rejt[1]),sum_rejt[2];
        ,sum_rejt[3]
    }
    fclose (fp);
}

Edited 5 Years Ago by Narue: added code tags and formatting

Comments
This is not a valid post on DaniWeb
// log_analyser.cpp : Defines the entry point for the console application.
//
#include<stdafx.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define MAX 500

int pnc1(FILE *fp)
{
	int i,j,k,tot;
	char a,b,c;
	a=fgetc(fp);
	i=(a-'0');
	b=fgetc(fp);
	j=(b-'0');
	c=fgetc(fp);
	k=(c-'0');
	tot=(100 * i) + (10 * j) + k;
	return(tot);
}

int pnc2(FILE *fp)
{
	int i,j,tot;
	char a,b;
	a=fgetc(fp);
	i=(a-'0');
	b=fgetc(fp);
	j=(b-'0');
	tot=(10 * i) + j;
	fseek(fp,1,SEEK_CUR);
	return(tot);
}


int main ()
{
  FILE *fp;
  char c;
  char temp1,temp2;
  int req_A[MAX],req_B[MAX],req_C[MAX],req_D[MAX],disp_A[MAX],disp_B[MAX],disp_C[MAX],disp_D[MAX],rejt_A[MAX],rejt_B[MAX],rejt_C[MAX],rejt_D[MAX];
  int total_rejt_A=0, total_rejt_B=0, total_rejt_C=0, total_rejt_D=0;
  int total_req_A=0, total_req_B=0, total_req_C=0, total_req_D=0;
  int total_disp_A=0, total_disp_B=0, total_disp_C=0, total_disp_D=0;
  int i=0,sum=0,sum2=0,m;
  int flag=1;
  
  for(m=0;m<=MAX;m++)
  {
	  req_A[m]=0; req_B[m]=0; req_C[m]=0; req_D[m]=0;
	  disp_A[m]=0; disp_B[m]=0; disp_C[m]=0; disp_D[m]=0;
	  rejt_A[m]=0; rejt_B[m]=0; rejt_C[m]=0; rejt_D[m];
  }

  fp = fopen("temp.txt","r");
  if(fp==NULL) 
	  perror ("The following error occurred : ");
  else{
	  do{
	  c=fgetc(fp);
	  if(c=='%')
	  {
		  if(flag==1)
		  {
			  do{
			  temp1=fgetc(fp);
			  //printf("\n%c",temp1);
			  if(temp1=='A'){sum=pnc1(fp); req_A[i]=sum;printf("\n Notes Requested A = %d ",req_A[i]);} 
			  if(temp1=='B'){sum=pnc1(fp); req_B[i]=sum;printf("\n Notes Requested B = %d ",req_B[i]);}
			  if(temp1=='C'){sum=pnc1(fp); req_C[i]=sum;printf("\n Notes Requested C = %d ",req_C[i]);}
			  if(temp1=='D'){sum=pnc1(fp); req_D[i]=sum;printf("\n Notes Requested D = %d ",req_D[i]);}
		  }while(temp1!=' ');
		  flag=0;
		  }
		  else
		  {
			  fseek(fp,4,SEEK_CUR);
			  do{
				  temp2=fgetc(fp);
				  //printf("\n%c",temp2);
				  if(temp2=='A')
				  {
					  sum=pnc1(fp);
					  disp_A[i]=sum;
					  sum2=pnc2(fp);
					  rejt_A[i]=sum2;
					  printf("\n Notes Dispensed A = %d ",disp_A[i]);
					  printf("\n Notes Rejected A = %d",rejt_A[i]);
				  }
				  if(temp2=='B')
				  {
					  sum=pnc1(fp);
					  disp_B[i]=sum;
					  sum2=pnc2(fp);
					  rejt_B[i]=sum2;
					  printf("\n Notes Dispensed B = %d ",disp_B[i]);
					  printf("\n Notes Rejected B = %d",rejt_B[i]);
				  }
				  if(temp2=='C')
				  {
					  sum=pnc1(fp);
					  disp_C[i]=sum;
					  sum2=pnc2(fp);
					  rejt_C[i]=sum2;
					  printf("\n Notes Dispensed C = %d ",disp_C[i]);
					  printf("\n Notes Rejected C = %d",rejt_C[i]);
				  }
				  if(temp2=='D')
				  {
					  sum=pnc1(fp);
					  disp_D[i]=sum;
					  sum2=pnc2(fp);
					  rejt_D[i]=sum2;
					  printf("\n Notes Dispensed D = %d ",disp_D[i]);
					  printf("\n Notes Rejected D = %d",rejt_D[i]);
				  }
			  }while(temp2!=' ');
			  flag=1;
			  printf("\n");
		    }
		  i++;
	  }
	  }while(!feof(fp));

printf ("\n\n-------- %d  %d------\n\n",req_C[0],req_C[1]);
printf ("\n\n-------- %d  %d------\n\n",disp_C[0],disp_C[1]);

//---------------------------------

//-----------------
//printing values

printf("\n\n Number of times Dispenser called : %d",i/2);
printf("\n\n Number of notes Requested\n \t TYPE_1 %d\n \t TYPE_2 %d\n \t TYPE_3 %d\n \t TYPE_4 %d\n",total_req_A, total_req_B, total_req_C, total_req_D);
printf("\n\n Number of notes Dispensed\n \t TYPE_1 %d\n \t TYPE_2 %d\n \t TYPE_3 %d\n \t TYPE_4 %d\n",total_disp_A, total_disp_B, total_disp_C, total_disp_D);
printf("\n\n Number of notes Rejected\n \t TYPE_1 %d\n \t TYPE_2 %d\n \t TYPE_3 %d\n \t TYPE_4 %d\n",total_rejt_A, total_rejt_B, total_rejt_C, total_rejt_D);

	  fclose(fp);
  }
  getch();
  return 0;
}

Edited 5 Years Ago by Narue: added code tags

Comments
Thanks, that's very generous of you!

There is no excuse not to say anything in a help forum. We are not robots: we don't communicate in code. If you are not comfortable with the English language, its okay. We will not criticise your speech. But you must explain yourself, otherwise you are just spamming the site.

Maybe these are just code snippets. :|

There is a right way to post code snippets. This is the wrong way, primarily because he doesn't describe the application of the code he is posting.

It would be nice if a moderator can provide a link on the rules for publishing code snippets so I can point out the specific violations. (I could not locate them myself)

how can i delete this post........ any idea??

Well, since you know English, why are you posting this code?

i just posted for some experiment........... anyways, my problem is solved..... but now how to delete this thread or post....???

This question has already been answered. Start a new discussion instead.