Program for changing records of one file with another.

#include<stdio.h>
#include<stdlib.h>
void strparse(char *buf, char *[]);

int main(int argc,char *argv[])
{
FILE *fp1 = NULL,*fp2 = NULL;

int eno,eage, eno1,eage1,i=0,j=0,count=0;
char line[80],enm[26],enm1[26],buf[80],buf1[80],*str1[80]={0,} , *str2[80] = {0,},*tempb,*stri[25];
double esal=0.0,esal1=0.0;

if(argc!=2)
{
printf("Usage: transaction <file name>");
exit(0);
}

fp1=fopen(argv[1],"r");
fp2=fopen("emp.fix","r");

if(!fp1 || !fp2)
{
printf(" Error in file open \n");
exit(0);
}

while(fgets(buf,80,fp1))
{
stri[i]=(char *)malloc(80);
if (fgets(buf1,80,fp2))
{
strparse(buf,str1);
eno=atoi(str1[0]);
strcpy(enm,str1[1]);
eage=atoi(str1[2]);
esal=atof(str1[3]);

strparse(buf1,str2);
eno1=atoi(str2[0]);
strcpy(enm1,str2[1]);
eage1=atoi(str2[2]);
esal1=atof(str2[3]);

if(eno1==eno)
{
if(enm)
strcpy(enm1,enm);
if(eage!=0)
eage1=eage;
if(esal!=0)
esal1=esal;
}
sprintf(stri[i],"%04d|%s|%02d|%7.2f\n",eno1,enm1,eage1,esal1);
i++;
}
}
fclose(fp1);
fclose(fp2);
fp2=fopen("emp.fix","w");
for(j=0;j<i;j++)
fprintf(fp2,"%s",stri[j]);
}

void  strparse(char *buf,char *str[])
{
int i=0,j=0;
char *tempb;
for(tempb=buf; *tempb!=''; tempb++)
{
if(!str[j])
str[j]=(char*)malloc(100);

if(*tempb!='|')
{
*(str[j]+i)=*tempb;
i++;
}
else
{
*(str[j]+i)='';
j++;
i=0;
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s