scanf allows us to read a single word but not multiple words.
gets allows us to read multiple words but only one string
In Let Us C chap 9 page 334, there is a solution to readng multiple words and strings using : scanf("%[^\n]s",word);
However when we want to read from a text file we cannot use
fscanf(file,"%[^\n]s",word); to read a multiple word string as it is logically not possible to do so.
fscanf only allows us to read one word at a time. This is due to the fact that the format of how the text are arranged in the text file are arranged and defined by the user.
For example, lets say we have a text file having an address and the number of houses in that location.
If we have an address like "Green Park 22" and the number of houses here in the location is 50, the format of the text file would be something like this:
"Green Park 22 50"
If you give this information to a stranger, he/she wouldn't know what the data is about. It might be that "Green Park" is the address, "22" is the number of houses and
"50" is the number of inhabitants or that "Green" is a country, "Park" is a district and "22 50" is the house number.
Therefore to read the text file in a proper manner, it has to be done by the user.
In the example below, the program stores students names and the scores they have.
In this program,unlike all the other solutions previously, the students' names can have multiple words(Up to 3 though it can be edited to accomodate more words) e.g "Mr Multiple". For this program you have to
1)Create a folder name "ctxt" in C: Drive
2)Create a new text file named "11TEST".
3)Then you would have the directory C:\\ctxt\\11TEST.txt as required by the program.
The format of the data in the text file is:
Name Marks
The method used is to detect the spacing between the name of the student and his/her marks. To do this, i have applied a double spacing between the name and marks so when the program detects this double spacing, it knows the next data it reads is marks.
Below is the solution:
#include "stdio.h"
struct data
{
char name[30];
int numb;
};
void main()
{
FILE *fs;
struct data d;
int choice,i,f;
long pos;
char another,ch,word[3][30];
fs=fopen("c:\\ctxt\\11TEST.txt","r+");
while(1)
{
clrscr();
printf("1) Key in new data");
printf("\n2) List down the data keyed");
printf("\n3) Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
clrscr();
fseek(fs,0,SEEK_END);
while(1)
{
clrscr();
another='n';
printf("Key in name\n");
fflush(stdin);
scanf("%[^\n]s",d.name);
printf("Key in score\n");
scanf("%d",&d.numb);
fprintf(fs,"%s(double spacing here) %d\n",d.name,d.numb);
clrscr();
printf("Do you want to key in another record?(y/n)");
fflush(stdin);
another=getche();
if(another!='y')
break;
}
break;
case 2:
clrscr();
fseek(fs,0,SEEK_SET);
while(ch!=EOF)
{
d.name[0]='\0';
word[0][0]='\0';
word[1][0]='\0';
word[2][0]='\0';
f=0;
for(i=1;ch!=EOF,f==0;i++)
{
ch=fscanf(fs,"%s",word[i-1]);
ch=fgetc(fs);
if(ch==32)
{
f=0;
}
else
{
f=1;
break;
}
ch=fgetc(fs);
if(ch!=32)
{
f=0;
}
else
{
f=2;
break;
}
fseek(fs,-2,SEEK_CUR);
}
if(f==2)
fseek(fs,-2,SEEK_CUR);
if(i==2)
{
strcat(word[0]," ");
strcat(word[0],word[1]);
}
if(i==3)
{
strcat(word[0]," ");
strcat(word[0],word[1]);
strcat(word[0]," ");
strcat(word[0],word[2]);
}
strcpy(d.name,word[0]);
if(d.name[0]=='\0')
break;
printf("%s",d.name);
ch=fscanf(fs,"%d",&d.numb);
printf(" %d\n",d.numb);
if(ch==EOF)
break;
}
case 3:
fclose(fs);
exit();
}
}
}
Saturday, January 23, 2010
Monday, November 30, 2009
Chap 13[B](a) Reverse words in text file
Write a program to carry out the following:
(a) Read a text file provided at command prompt
(b) Print each word in reverse order
For example if the file contains
INDIA IS MY COUNTRY
Output should be
AIDNI SI YM YRTNUOC
How to use:
1) Compile the below program to obtain .exe file.
2) In turbo C press F9 to compile and go to C:\TC\BIN to find your .exe file
3) Rename your.exe file to any name for example reverse.exe.
4) In the folder where reverse.exe is, create a text file (e.g test.txt) and input any text in it.
5) For vista, in the same folder hold shift and right click anywhere in the folder and click open command prompt here.
6) Then in command prompt type :
reverse test.txt test2.txt
7)The reversed text is found in test2.txt.
#include "stdio.h"
void main(int argc,char *argv[])
{
int i,j,f=1;
FILE *fs,*ft;
char ch,word[25],word2[25];
if(argc!=3)
{
puts("Improper number of arguments");
exit();
}
fs=fopen(argv[1],"r");
if(fs==NULL)
{
puts("\nCannot open source file");
exit();
}
ft=fopen(argv[2],"w");
while(1)
{
word2[0]='\0';
word[0]='\0';
for(i=0;;i++)
{
ch=fgetc(fs);
if(ch==EOF)
{
f=0;
break;
}
if(ch==32)
{
f=2;
break;
}
if(ch==44)
{
f=3;
break;
}
if(ch==46)
{
f=4;
break;
}
if(ch=='\n')
{
f=5;
break;
}
word[i]=ch;
}
word[i]='\0';
for(j=0,i-=1;i>=0;i--,j++)
word2[j]=word[i];
word2[j]='\0';
fputs(word2,ft);
if(f==2)
fputc(32,ft);
if(f==3)
fputc(44,ft);
if(f==4)
fputc(46,ft);
if(f==5)
fputc('\n',ft);
if(f==0)
break;
}
fclose(fs);
fclose(ft);
}
(a) Read a text file provided at command prompt
(b) Print each word in reverse order
For example if the file contains
INDIA IS MY COUNTRY
Output should be
AIDNI SI YM YRTNUOC
How to use:
1) Compile the below program to obtain .exe file.
2) In turbo C press F9 to compile and go to C:\TC\BIN to find your .exe file
3) Rename your.exe file to any name for example reverse.exe.
4) In the folder where reverse.exe is, create a text file (e.g test.txt) and input any text in it.
5) For vista, in the same folder hold shift and right click anywhere in the folder and click open command prompt here.
6) Then in command prompt type :
reverse test.txt test2.txt
7)The reversed text is found in test2.txt.
#include "stdio.h"
void main(int argc,char *argv[])
{
int i,j,f=1;
FILE *fs,*ft;
char ch,word[25],word2[25];
if(argc!=3)
{
puts("Improper number of arguments");
exit();
}
fs=fopen(argv[1],"r");
if(fs==NULL)
{
puts("\nCannot open source file");
exit();
}
ft=fopen(argv[2],"w");
while(1)
{
word2[0]='\0';
word[0]='\0';
for(i=0;;i++)
{
ch=fgetc(fs);
if(ch==EOF)
{
f=0;
break;
}
if(ch==32)
{
f=2;
break;
}
if(ch==44)
{
f=3;
break;
}
if(ch==46)
{
f=4;
break;
}
if(ch=='\n')
{
f=5;
break;
}
word[i]=ch;
}
word[i]='\0';
for(j=0,i-=1;i>=0;i--,j++)
word2[j]=word[i];
word2[j]='\0';
fputs(word2,ft);
if(f==2)
fputc(32,ft);
if(f==3)
fputc(44,ft);
if(f==4)
fputc(46,ft);
if(f==5)
fputc('\n',ft);
if(f==0)
break;
}
fclose(fs);
fclose(ft);
}
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+");
}
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+");
}
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");
}
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");
}
Saturday, November 14, 2009
Chap12[C]d Sorting to alphabetical order in a file
Suppose a file contains student’s records with each record containing name and age of a student. Write a program to read these records and display them in sorted order by name.
Program to input students name
#include "stdio.h"
void main()
{
char j='y';
FILE *fp;
struct record
{
char name[20];
int age;
};
struct record a;
fp=fopen("c:\\ctxt\\record.txt","a");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
while(j=='y')
{
printf("\nKey in the name of the student\n");
scanf("%s",a.name);
printf("\nKey in the age of the student\n");
scanf("%d",&a.age);
fprintf(fp,"%s %d\n",a.name,a.age);
printf("\nAdd another record?(Y/N)");
fflush(stdin);
j=getche();
}
fclose(fp);
}
Program to sort students name
#include "stdio.h"
struct record
{
char name[10];
int age;
};
void main()
{
FILE *fp,*ft;
int i,f=1;
struct record a[5000];
clrscr();
fp=fopen("c:\\ctxt\\record.txt","r");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
for(i=0;i<5000;i++)
{
if(f==1)
{
if((fscanf(fp,"%s%d",a[i].name,&a[i].age)==EOF))
f=0;
}
if(f==0)
a[i].name[0]='\0';
}
funcsort(a,4);
for(i=0;a[i].name[0]!='\0';i++)
printf("\nName is %s. Age is %d",a[i].name,a[i].age);
printf("\nFile sorted according to alphabetical order");
ft=fopen("c:\\ctxt\\record2.txt","w");
for(i=0;a[i].name[0]!=0;i++)
fprintf(ft,"%s %d\n",a[i].name,a[i].age);
fclose(fp);
fclose(ft);
remove("c:\\ctxt\\record.txt");
rename("c:\\ctxt\\record2.txt","c:\\ctxt\\record.txt");
}
/* Function "funcsort()" is in picture below type it in for it to work */
Program to input students name
#include "stdio.h"
void main()
{
char j='y';
FILE *fp;
struct record
{
char name[20];
int age;
};
struct record a;
fp=fopen("c:\\ctxt\\record.txt","a");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
while(j=='y')
{
printf("\nKey in the name of the student\n");
scanf("%s",a.name);
printf("\nKey in the age of the student\n");
scanf("%d",&a.age);
fprintf(fp,"%s %d\n",a.name,a.age);
printf("\nAdd another record?(Y/N)");
fflush(stdin);
j=getche();
}
fclose(fp);
}
Program to sort students name
#include "stdio.h"
struct record
{
char name[10];
int age;
};
void main()
{
FILE *fp,*ft;
int i,f=1;
struct record a[5000];
clrscr();
fp=fopen("c:\\ctxt\\record.txt","r");
if(fp==NULL)
{
puts("Cannot open file");
exit();
}
for(i=0;i<5000;i++)
{
if(f==1)
{
if((fscanf(fp,"%s%d",a[i].name,&a[i].age)==EOF))
f=0;
}
if(f==0)
a[i].name[0]='\0';
}
funcsort(a,4);
for(i=0;a[i].name[0]!='\0';i++)
printf("\nName is %s. Age is %d",a[i].name,a[i].age);
printf("\nFile sorted according to alphabetical order");
ft=fopen("c:\\ctxt\\record2.txt","w");
for(i=0;a[i].name[0]!=0;i++)
fprintf(ft,"%s %d\n",a[i].name,a[i].age);
fclose(fp);
fclose(ft);
remove("c:\\ctxt\\record.txt");
rename("c:\\ctxt\\record2.txt","c:\\ctxt\\record.txt");
}
/* Function "funcsort()" is in picture below type it in for it to work */
Thursday, November 12, 2009
Chap12[C]g Reading text on console with pages
Write a program to display the contents of a text file on the screen. Make following provisions: Display the contents inside a box drawn with opposite corner co-ordinates being ( 0, 1 ) and ( 79, 23 ). Display the name of the file whose contents are being displayed, and the page numbers in the zeroth row. The moment one screenful of file has been displayed, flash a message ‘Press any key...’ in 24th row. When a key is hit, the next page’s contents should be displayed, and so on till the end of file.
Create a folder called "ctxt" in C drive and create a text file called page.txt in the ctxt folder.
Input lots of words into the page.txt file then run this program.
#include "stdio.h"
#include "dos.h"
void main()
{
void creategrid();
FILE *fp;
char ch,k;
int page=1,i,j,pgno,flag=0,count;
fp=fopen("c:\\ctxt\\page.txt","r");
while(1)
{
count=0;
clrscr();
creategrid();
for(i=3;i<=21&&flag!=1;i++)
{
for(j=4;j<=77&&flag!=1;j++)
{
ch=fgetc(fp);
if(ch==EOF)
{
flag=1;
break;
}
gotoxy(j,i);
printf("%c",ch);
count++;
}
}
gotoxy(38,24);
printf("Page %d",page);
if(page>1)
{
gotoxy(3,24);
printf("Key %c to go to page %d",17,page-1);
}
if(flag!=1)
{
gotoxy(58,24);
printf("Key %c to go to page %d",16,page+1);
}
if(flag==1)
{
gotoxy(66,24);
printf("Key %c to quit",16);
}
while(1)
{
pgno=getkey();
if(flag==1&&pgno==77)
break;
if(page==1&&pgno==75)
continue;
if(pgno==77&&flag!=1)
{
page+=1;
clrscr();
creategrid();
break;
}
if(pgno==75&&page!=1)
{
page-=1;
clrscr();
fseek(fp,-(1413+count),SEEK_CUR);
creategrid();
flag=0;
break;
}
}
if(flag==1&&pgno==77)
break;
}
fclose(fp);
}
getkey()
{
union REGS i,o;
while(!kbhit())
;
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void creategrid()
{
int i;
gotoxy(2,1);
printf("%c",218);
gotoxy(79,1);
printf("%c",191);
gotoxy(2,23);
printf("%c",192);
gotoxy(79,23);
printf("%c",217);
for(i=3;i<=78;i++)
{
gotoxy(i,1);
printf("%c",196);
gotoxy(i,23);
printf("%c",196);
}
for(i=2;i<=22;i++)
{
gotoxy(2,i);
printf("%c",179);
gotoxy(79,i);
printf("%c",179);
}
}
Create a folder called "ctxt" in C drive and create a text file called page.txt in the ctxt folder.
Input lots of words into the page.txt file then run this program.
#include "stdio.h"
#include "dos.h"
void main()
{
void creategrid();
FILE *fp;
char ch,k;
int page=1,i,j,pgno,flag=0,count;
fp=fopen("c:\\ctxt\\page.txt","r");
while(1)
{
count=0;
clrscr();
creategrid();
for(i=3;i<=21&&flag!=1;i++)
{
for(j=4;j<=77&&flag!=1;j++)
{
ch=fgetc(fp);
if(ch==EOF)
{
flag=1;
break;
}
gotoxy(j,i);
printf("%c",ch);
count++;
}
}
gotoxy(38,24);
printf("Page %d",page);
if(page>1)
{
gotoxy(3,24);
printf("Key %c to go to page %d",17,page-1);
}
if(flag!=1)
{
gotoxy(58,24);
printf("Key %c to go to page %d",16,page+1);
}
if(flag==1)
{
gotoxy(66,24);
printf("Key %c to quit",16);
}
while(1)
{
pgno=getkey();
if(flag==1&&pgno==77)
break;
if(page==1&&pgno==75)
continue;
if(pgno==77&&flag!=1)
{
page+=1;
clrscr();
creategrid();
break;
}
if(pgno==75&&page!=1)
{
page-=1;
clrscr();
fseek(fp,-(1413+count),SEEK_CUR);
creategrid();
flag=0;
break;
}
}
if(flag==1&&pgno==77)
break;
}
fclose(fp);
}
getkey()
{
union REGS i,o;
while(!kbhit())
;
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void creategrid()
{
int i;
gotoxy(2,1);
printf("%c",218);
gotoxy(79,1);
printf("%c",191);
gotoxy(2,23);
printf("%c",192);
gotoxy(79,23);
printf("%c",217);
for(i=3;i<=78;i++)
{
gotoxy(i,1);
printf("%c",196);
gotoxy(i,23);
printf("%c",196);
}
for(i=2;i<=22;i++)
{
gotoxy(2,i);
printf("%c",179);
gotoxy(79,i);
printf("%c",179);
}
}
Subscribe to:
Posts (Atom)