Sunday, November 15, 2009

Chap12[C]j Sort data in file according to date

There are 100 records present in a file with the following structure:
struct date
{
int d, m, y ;
} ;
struct employee
{ int empcode[6] ;
char empname[20] ;
struct date join_date ;
float salary ;
} ;
Write a program to read these records, arrange them in ascending order of join_date and write them in to a target file.


#include "stdio.h"
struct date
{
int d,m,y;
};
struct employee
{
int empcode;
char empname[20];
struct date da;
float salary;
};

void main()
{
int choice;
char another,another2;
FILE *fs;
struct employee e;
fs=fopen("c:\\ctxt\\join.txt","a+");
while(1)
{
clrscr();
printf("1) Input new employee data");
printf("\n2) Sort employee data");
printf("\n3) Exit");
printf("\n\nKey in your choice(1,2 or 3)\n");
scanf("%d",&choice);

switch(choice)
{
case 1:
while(1)
{
clrscr();
printf("\nKey in employee's code\n");
scanf("%d",&e.empcode);
printf("\nKey in employee's name\n");
scanf("%s",e.empname);
printf("\nKey in year that employee joined\n");
scanf("%d",&e.da.y);
printf("\nKey in month that employee joined\n");
scanf("%d",&e.da.m);
printf("\nKey in day that employee joined\n");
scanf("%d",&e.da.d);
printf("\nKey in salary of employee\n");
scanf("%f",&e.salary);
fprintf(fs,"%d %s %d %d %d %f\n",e.empcode,e.empname,e.da.y,e.da.m,e.da.d,e.salary);
printf("\nDo you want to key in another data?(y/n)\n");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
break;

case 2:
funcsort(fs);
break;

case 3:
fclose(fs);
exit();
}
}
}

funcsort(FILE *fs)
{
int i,j,flag=0,count=0;
struct employee temp1,temp2;
struct employee e1[1000];
char ch;
FILE *fp;
fp=fopen("c:\\ctxt\\join2.txt","w");
rewind(fs);
for(i=0;i<1000;i++)
{
if(flag==0)
{
ch=fscanf(fs,"%d%s%d%d%d%f",&e1[i].empcode,e1[i].empname,&e1[i].da.y,&e1[i].da.m,&e1[i].da.d,&e1[i].salary);
count++;
if(ch==EOF)
flag=1;
}
if(flag==1)
{
e1[i].empname[0]='\0';
}
}
for(i=0;e1[i].empname[0]!=0;i++)
{
for(j=i+1;j {
if(e1[i].da.y>e1[j].da.y)
{
temp1=e1[i];
temp2=e1[j];
e1[i]=temp2;
e1[j]=temp1;
}
else if((e1[i].da.y==e1[j].da.y)&&(e1[i].da.m==e1[j].da.m))
{
if(e1[i].da.d>e1[j].da.d)
{
temp1=e1[i];
temp2=e1[j];
e1[i]=temp2;
e1[j]=temp1;
}
}
else if(e1[i].da.y==e1[j].da.y)
{
if(e1[i].da.m>e1[j].da.m)
{
temp1=e1[i];
temp2=e1[j];
e1[i]=temp2;
e1[j]=temp1;
}
}

}
}

for(i=0;e1[i].empname[0]!=0;i++)
{
fprintf(fp,"%d %s %d %d %d %f\n",e1[i].empcode,e1[i].empname,e1[i].da.y,e1[i].da.m,e1[i].da.d,e1[i].salary);
}
fclose(fp);
fclose(fs);
remove("c:\\ctxt\\join.txt");
rename("c:\\ctxt\\join2.txt","c:\\ctxt\\join.txt");
fs=fopen("c:\\ctxt\\join.txt","a+");
}

Chap12[C]i Updating customer transactions with sorting feature

In the file ‘CUSTOMER.DAT’ there are 100 records with the following structure:
struct customer
{
int accno ;
char name[30] ;
float balance ;
} ;

In another file ‘TRANSACTIONS.DAT’ there are several records with the following structure:
struct trans
{
int accno ;
char trans_type ;
float amount ;
} ;
The parameter trans_type contains D/W indicating deposit or withdrawal of amount. Write a program to update ‘CUSTOMER.DAT’ file, i.e. if the trans_type is ‘D’ then update the balance of ‘CUSTOMER.DAT’ by adding amount to balance for the corresponding accno. Similarly, if trans_type is ‘W’ then subtract the amount from balance. However, while subtracting the amount make sure that the amount should not get overdrawn, i.e. at least 100 Rs. Should remain in the account.


Create a folder "ctxt" in C drive. In the "ctxt" folder create two text files with name cust.txt and trans.txt.
when option 4 is executed trans.txt get deleted while new transactions can be created with option 5 in program.

#include "stdio.h"
struct customer
{
int accno;
char name[30];
float balance;
};

struct trans
{
int accno;
char trans_type[1];
float amount;
};

void main()
{
char another,name1[30];
FILE *fs,*ft,*ftemp;
struct customer c;
struct trans t;
int choice,no,f2,no2,counter;
long pos1,pos2,pos3;
fs=fopen("c:\\ctxt\\cust.txt","r+");

if(fs==NULL)
{
puts("Cannot open file");
exit();
}

while(1)
{
clrscr();
printf("1)List Customer Data");
printf("\n2)Add Customer Data");
printf("\n3)Remove Customer Data");
printf("\n4)Update Customer Data from transactions");
printf("\n5)Perform transactions");
printf("\n6)Exit\n");
scanf("%d",&choice);
rewind(fs);

switch(choice)
{
case 1:

another='y';
clrscr();
while(fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF)
printf("\nAccount no: %3d Name: %-20s Balance: %.1f",c.accno,c.name,c.balance);
while(1)
{
printf("\n\nHit enter to escape");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
break;

case 2:

clrscr();
another='y';
no=1;
funcsort(fs);
while(1)
{

f2=0;

for(no=1;fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF;no++)
{
if(f2==0)
{
if(c.accno!=no)
{
no2=no;
f2=1;
}

}

}
printf("\nKey in Customer name\n");
scanf("%s",c.name);
printf("\nKey in balance\n");
scanf("%f",&c.balance);
if(f2==1)
{
fprintf(fs,"%d %s %f\n",no2,c.name,c.balance);
}
else
{
fprintf(fs,"%d %s %f\n",no,c.name,c.balance);
no++;
}
funcsort(fs);
printf("\nAdd another customer Data(y/n)?\n");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
break;

case 3:

another='y';
clrscr();

while(1)
{
ftemp=fopen("c:\\ctxt\\cust2.txt","w");
printf("\nKey in name to delete\n");
scanf("%s",name1);
while(fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF)
{
if(strcmp(c.name,name1)!=0)
fprintf(ftemp,"%d %s %f\n",c.accno,c.name,c.balance);
}
fclose(fs);
fclose(ftemp);
remove("c:\\ctxt\\cust.txt");
rename("c:\\ctxt\\cust2.txt","c:\\ctxt\\cust.txt");
printf("\nDelete another data?(y/n)\n");
fflush(stdin);
fs=fopen("c:\\ctxt\\cust.txt","r+");
another=getche();
if(another!='y')
break;
}
break;

case 4:

clrscr();
ft=fopen("c:\\ctxt\\trans.txt","r");
if(ft==NULL)
{
puts("No active transactions");
exit();
}
while(fscanf(ft,"%d%s%f",&t.accno,t.trans_type,&t.amount)!=EOF)
{
counter=0;
pos1=0;
rewind(fs);
while(fscanf(fs,"%d%s%f",&c.accno,c.name,&c.balance)!=EOF)
{
counter++;
pos2=pos1;
pos1=ftell(fs);
pos3=pos1-pos2;
if(t.accno==c.accno)
{
if(t.trans_type[0]=='w')
{
c.balance-=t.amount;
if(c.balance<100)
{
c.balance+=t.amount;
printf("\nTransaction for account no: %d not processed due to insufficient funds");
}
}
if(t.trans_type[0]=='d')
{
c.balance+=t.amount;

}
if(counter==1)
fseek(fs,-(pos3),SEEK_CUR);
if(counter>1)
fseek(fs,-(pos3-2),SEEK_CUR);
fprintf(fs,"%d %s %f\n",c.accno,c.name,c.balance);
counter=1;
}

}
}
fclose(ft);
remove("c:\\ctxt\\trans.txt");
break;

case 5:

clrscr();
another='y';
ft=fopen("c:\\ctxt\\trans.txt","a+");
if(ft==NULL)
{
puts("Cannot open file");
break;
}
while(1)
{
printf("\nKey in acc no.\n");
scanf("%d",&t.accno);
printf("\nWithdraw (w) or Deposit (d)\n");
scanf("%s",t.trans_type);
printf("\nKey in amount\n");
scanf("%f",&t.amount);
fprintf(ft,"%-3d %s %-6.2f\n",t.accno,t.trans_type,t.amount);
printf("\nDo you want to key another transaction(y/n)?\n");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
fclose(ft);
break;

case 6:

fclose(fs);
exit();

}
}
fclose(fs);
}

funcsort(FILE *fp)
{
char ch;
int flag=0,i,j,count;
struct customer a[1000],temp1,temp2;
FILE *ftemp;
rewind(fp);
ftemp=fopen("c:\\ctxt\\cust2.txt","w");
for(i=0,count=0;i<1000;i++)
{
if(flag==0)
{
ch=fscanf(fp,"%d%s%f",&a[i].accno,a[i].name,&a[i].balance);
count++;
if(ch==EOF)
{
flag=1;
}
}
if(flag==1)
{
a[i].name[0]=0;
}
}

for(i=0;a[i].name[0]!=0;i++)
{
for(j=i+1;j<(count-1);j++)
{
if(a[i].accno>a[j].accno)
{
temp1=a[i];
temp2=a[j];
a[i]=temp2;
a[j]=temp1;
}
}
}

for(i=0;a[i].name[0]!=0;i++)
{
fprintf(ftemp,"%d %s %f\n",a[i].accno,a[i].name,a[i].balance);
}
fclose(fp);
fclose(ftemp);
remove("c:\\ctxt\\cust.txt");
rename("c:\\ctxt\\cust2.txt","c:\\ctxt\\cust.txt");
fp=fopen("c:\\ctxt\\cust.txt","r+");
}

Chap12[C]h Encrypting/Decrypting file

Write a program to encrypt/decrypt a file using:
An offset cipher: In an offset cipher each character from the source file is offset with a fixed value and then written to the target file. For example, if character read from the source file is ‘A’, then convert this into a new character by offsetting ‘A’ by a fixed value, say 128, and then writing the new character to the target file.


Program to encrypt file

#include "stdio.h"
void main()
{
FILE *fp,*ft;
char ch;

fp=fopen("c:\\ctxt\\cipher.txt","r");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
ft=fopen("c:\\ctxt\\cipher2.txt","w");
if(ft==NULL)
{
puts("Cannot open file");
exit();
}

while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
ch+=128;
fputc(ch,ft);
};
fclose(fp);
fclose(ft);
remove("c:\\ctxt\\cipher.txt");
rename("c:\\ctxt\\cipher2.txt","c:\\ctxt\\cipher.txt");
}

Program to decrypt the same file


#include "stdio.h"
void main()
{
FILE *fp,*ft;
char ch;
fp=fopen("c:\\ctxt\\cipher.txt","r");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
ft=fopen("c:\\ctxt\\cipher2.txt","w");
if(ft==NULL)
{
puts("Cannot open file");
exit();
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
ch-=128;
fputc(ch,ft);
};
fclose(fp);
fclose(ft);
remove("c:\\ctxt\\cipher.txt");
rename("c:\\ctxt\\cipher2.txt","c:\\ctxt\\cipher.txt");
}