program for reading records from one file and writing them to another file in ‘pipe seperated fields’ format

#include<stdio.h>
#define MAXLINE 80
#define NAMELEN 25
pad(char *s,int len)
{
for(;*s && len;s++,len--);
for(;len;len--)
*s++=' ';
*s='';
}
int main()
{
FILE *fp1,*fp2;
int eno,eage;
char line[MAXLINE],enm[NAMELEN+1];
double esal=0.0;
if(!(fp1=fopen("emp.var","r")))
{
printf("\ncannot open the file \" emp.var\"\n");
exit(0);
}
if(!(fp2=fopen("emp.fix","w")))
{
printf("\ncannot open the file \" emp.fix\"\n");
exit(0);
}
while(fscanf(fp1,"%d %s %d %lf",&eno,enm,&eage,&esal)!=EOF)
{
pad(enm,NAMELEN);
sprintf(line,"%04d|%25.25s|%02d|%7.2f",eno,enm,eage,esal);
fprintf(fp2,"%s\n",line);
}
    fclose(fp1);

fclose(fp2);
}

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