#include
#include
#include
static nc1,nc2;
char temp[25],P[10],Q[10],R[10],temp11[30];
struct node
{
char name[15];
char var[2];
int old,nev;
}T1,T2;
void main()
{
FILE *fp1,*fp2;
char ch,ch1,ch2,ch3,temp2[15],temp1[15];
int x,y,z,p,q,r;
clrscr();
redo();
fp1=fopen("database.txt","r+");
fp2=fopen("pritam.log","w+");
while(ch1!=EOF)
{
ch1=fgetc(fp1);
// printf("\n%c",ch1);
if(ch1=='A')
{
fgetc(fp1);
fgets(P,4,fp1);
// printf("\n%s",P);
p=atoi(P);
// printf("\n....%d\n",p);
// printf("\n %s",P);
}
if(ch1=='B')
{
fgetc(fp1);
fgets(Q,4,fp1);
q=atoi(Q);
// printf("\n....%d\n",q);
}
if(ch1=='C')
{
fgetc(fp1);
fgets(R,4,fp1);
r=atoi(R);
// printf("\n....%d\n",r);
}
}
fclose(fp1);
while(1)
{
printf("\nY- to begin & N- to abort");
nc1=1;
nc2=1;
T1.name[0]='T';
T1.name[1]='1';
T2.name[0]='T';
T2.name[1]='2';
scanf("%c",&ch);
if(ch=='y')
{
if(nc1==1)
{
strcpy(temp,T1.name);
strcat(temp," start");
//printf("\n %s",temp);
log(temp);
strcpy(temp,T1.name);
}
//printf("\n %s",P);
ask();
printf("\nread P");
//printf("\n %s",P);
ask();
printf("\nP=P-20");
ask();
T1.var[0]='P';
strcat(temp," ");
strcat(temp,T1.var);
strcat(temp," ");
strcat(temp,P);
// printf("\n%s",P);
p=p-20;
//printf("\n%d",p);
itoa(p,P,10);
strcat(temp," ");
strcat(temp,P);
// printf("\n%s",temp);
printf("\nwrite P");
update(T1.var,P);
log(temp);
ask();
strcpy(temp,T1.name);
printf("\nread Q");
ask();
printf("\nQ=Q+50");
ask();
T1.var[0]='Q';
strcat(temp," ");
strcat(temp,T1.var);
strcat(temp," ");
strcat(temp,Q);
//printf("...%s",Q);
q=q+50;
itoa(q,Q,10);
strcat(temp," ");
strcat(temp,Q);
//printf("\n%s",temp);
printf("\nwrite Q");
log(temp);
update(T1.var,Q);
ask();
strcpy(temp,T1.name);
printf("\nT1 commited");
strcat(temp," commit");
log(temp);
ask();
strcpy(temp,T2.name);
printf("\nT2 start");
strcat(temp," start");
log(temp);
ask();
printf("\nread R");
ask();
printf("\nR=R+100");
ask();
T2.var[0]='R';
strcpy(temp,T2.name);
strcat(temp," ");
strcat(temp,T2.var);
strcat(temp," ");
strcat(temp,R);
// printf("\n%d",r);
r=r+100;
printf("\n");
itoa(r,R,10);
strcat(temp," ");
strcat(temp,R);
//printf("\n%s",temp);
printf("\nwrite R");
log(temp);
update(T2.var,R);
ask();
strcpy(temp,T2.name);
strcat(temp," commit");
printf("\nT2 commited");
log(temp);
}
else
{
break;
}
}
fclose(fp1);
fclose(fp2);
getch();
}
log()
{
FILE *fp3;
char temp4[40];
fp3=fopen("pritam.log","a");
fputs("< ",fp3);
fputs(temp,fp3);
fputs(" >\n",fp3);
fclose(fp3);
return(0);
}
update()
{
FILE *fp4;
char ch2;
int x=80;
char temp2[5];
fp4=fopen("database.txt","r+");
rewind(fp4);
while(ch2!=EOF)
{
ch2=fgetc(fp4);
if(ch2=='A')
{
fseek(fp4,1,1);
fputs(P,fp4);
// fputc(' ',fp4);
fseek(fp4,5,0);
}
if(ch2=='B')
{
fseek(fp4,1,1);
fputs(Q,fp4);
fseek(fp4,11,0);
}
if(ch2=='C')
{
fseek(fp4,1,1);
fputs(R,fp4);
}
}
fclose(fp4);
return 0;
}
ask()
{
int ch;
printf("\n\nDo you want to continue 1=>yes 2=>no:");
scanf("%d",&ch);
if(ch==1)
{
return;
}
else
{
exit(0);
}
}
redo()
{
FILE *fp5;
int ctr=1;
char pe[25],qe[25],re[25],temp12[20],temp13[25],temp14[25];
char ch,ch1;
fp5=fopen("pritam.log","r+");
printf(" In redo");
//fputs("pritam",fp5);
//rewind(fp5);
while(ch=fgetc(fp5)!=EOF)
{
if(ctr==1)
fseek(fp5,0l,0);
fgets(temp11,13,fp5);
if(strcmp(temp11,"< T1 start >")==0)
{
fseek(fp5,56,0);
// fseek(fp5,-3,1);
fgets(temp12,14,fp5);
// printf("\n.....%s",temp12);
if(strcmp(temp12,"< T1 commit >")==0)
{
fseek(fp5,12,0);
// printf("\n....aaa");
while(ch1!=EOF)
{
ch1=fgetc(fp5);
if(ch1=='P')
{
fseek(fp5,7,1);
fgets(pe,4,fp5);
strcpy(P,pe);
update();
// printf("\n\nRED1 is %s",pe);
}
if(ch1=='Q')
{
fseek(fp5,7,1);
fgets(qe,4,fp5);
strcpy(Q,qe);
update();
// printf("\n\nRED11 is %s",qe);
}
}
}
else
{
fseek(fp5,12,0);
// printf("\n....aaa");
while(ch1!=EOF)
{
ch1=fgetc(fp5);
if(ch1=='P')
{
fseek(fp5,2,1);
fgets(pe,4,fp5);
strcpy(P,pe);
update();
// printf("\n\nRED1 is %s",pe);
}
if(ch1=='Q')
{
fseek(fp5,2,1);
fgets(qe,4,fp5);
strcpy(Q,qe);
update();
// printf("\n\nRED11 is %s",qe);
}
}
}
ctr=2;
}
if(ctr==2)
fseek(fp5,71,0);
fgets(temp13,13,fp5);
// printf(".\n%s",temp13);
if(strcmp(temp13,"< T2 start >")==0)
{
fseek(fp5,106,0);
// fseek(fp5,-3,1);
fgets(temp14,14,fp5);
// printf("\n%s",temp14);
if(strcmp(temp14,"< T2 commit >")==0)
{
// printf("\n....aaa");
fseek(fp5,85,0);
//fseek(fp5,0l,0);
while(ch1!=EOF)
{
ch1=fgetc(fp5);
if(ch1=='R')
{
fseek(fp5,13,1);
fgets(re,4,fp5);
strcpy(R,re);
update();
//printf("\n\nRED111 is %s",re);
}
}
}
else
{
fseek(fp5,85,0);
//fseek(fp5,0l,0);
while(ch1!=EOF)
{
ch1=fgetc(fp5);
if(ch1=='R')
{
fseek(fp5,8,1);
fgets(re,4,fp5);
strcpy(R,re);
update();
//printf("\n\nRED111 is %s",re);
}
}
}
ctr=3;
ch=fgetc(fp5);
}
}
}
Copyright © 2011 - All Rights Reserved - Softron.in
Template by Softron Technology