Académique Documents
Professionnel Documents
Culture Documents
Exercise [C]
---------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
NOTE: Make two file as "FILE1.TXT" and "FILE2.TXT" in bin directory and then run the program.
The contents of FILE1.TXT will be appended to FILE2.TXT.
#include<stdio.h>
#include<conio.h>
void main() {
FILE *f1,*f2;
char ch;
clrscr();
f1=fopen("FILE1.TXT","r"); /* file to append */
f2=fopen("FILE2.TXT","a+"); /* file to be appended */
if(f1==NULL || f2==NULL) {
printf("\ncannot open one of files!");
exit();
}
while(1) {
ch=fgetc(f1);
if(ch==EOF) {
break;
}
fputc(ch,f2);
}
fclose(f1);
fclose(f2);
printf("\ntask completed successfully!");
getch();
}
---------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#define N 100
struct student {
char name[30];
int age;
};
void main() {
struct student std;
struct student s[N]; /* size of array of structure defined globally for
convenience */
FILE *fp;
int flag=0,ch,i=0,count=0;
long recsize;
char another='y';
void srt_print(); /* funtion to sort and print */
clrscr();
recsize=sizeof(std);
fp=fopen("STUDENT.DAT","rb+");
if(fp==NULL) {
fp=fopen("STUDENT.DAT","wb+");
if(fp==NULL)
exit();
}
while(1) {
clrscr();
printf("\t\t\tStudent database\n");
printf("\t\t\t****************\n\n\n");
printf("\t\t\n1: Add student data\n");
printf("\t\t\n2: List student data\n");
printf("\t\t\n0: Exit");
gotoxy(2,24);
printf("Your choice: ");
scanf("%d",&ch);
switch(ch) {
case 1:
clrscr();
while(another=='y' || another=='Y') {
clrscr();
if(flag==0) {
printf("\n\n\nNo data found!\n");
}
printf("\n\n\npress any key to return...");
getch();
break;
case 0:
fclose(fp);
exit();
default:
printf("wrong input!\n");
exit();
}
}
}
/******** main ends ************/
/**** sorting and printing function ****/
void srt_print(struct student *ss, int n) {
struct student temp;
int i,j;
for(i=0;i<=n;i++) {
for(j=i+1;j<n;j++) {
/* checking first alphabets of both names */
if(ss[i].name[0] > ss[j].name[0]) {
temp=ss[i];
ss[i]=ss[j];
ss[j]=temp;
}
/* if first alphabets are same */
else if(ss[i].name[0]==ss[j].name[0] && ss[i].name[1] > ss[j].name[1]) {
temp=ss[i];
ss[i]=ss[j];
ss[j]=temp;
}
/* if first 2 alphabets are same */
else if(ss[i].name[0]==ss[j].name[0] && ss[i].name[1]==ss[j].name[1]) {
if(ss[i].name[2] > ss[j].name[2]) {
temp=ss[i];
ss[i]=ss[j];
ss[j]=temp;
}
}
fclose(fr);
fclose(fw);
printf("Task completed!");
getch();
}
----------------------------------------------------------------------------------------------------------------
void print();
void main() {
int inhandle,bytes,pg=1;
FILE *fp;
char source[80],buffer[1400];
clrscr();
printf("Enter file name: ");
gets(source);
inhandle=open(source, O_RDONLY);
if(inhandle==-1) {
printf("cannot open file!");
exit();
}
clrscr();
while(1) {
bytes=read(inhandle,buffer,1387);
if(bytes>0) {
gotoxy(32,1);
/* showing filename */
printf("%s",strupr(source));
gotoxy(70,1);
printf("Pg: %3d",pg);
}
else {
gotoxy(70,1);
printf("Pg: %3d",pg);
break;
}
++pg;
}
close(inhandle);
getch();
}
/********************************/
/* function to print the buffer */
/********************************/
void print(char s[1400]) {
int x=4,y=3,i=0;
while(s[i]!=EOF) {
gotoxy(x,y);
printf("%c",s[i]);
if(x>74) {
x=4;
y+=1;
}
if(y>21) {
gotoxy(2,24);
printf("press any key to go to next page...");
x=4;
y=3;
getch();
clrscr();
box();
}
x++;
i++;
}
}
/****************************/
/* function to draw the box */
/****************************/
void box() {
int i,j;
for(i=2;i<=77;i++) {
gotoxy(i,2);
printf("%c",196);
gotoxy(i,23);
printf("%c",196);
}
for(j=3;j<=22;j++) {
gotoxy(2,j);
printf("%c",179);
gotoxy(77,j);
printf("%c",179);
}
gotoxy(77,23);
printf("%c",217);
gotoxy(77,2);
printf("%c",191);
gotoxy(2,23);
printf("%c",192);
gotoxy(2,2);
printf("%c",218);
}
----------------------------------------------------------------------------------------------------------------
/* every character has been added to 128 and the new value has been
written */
FILE *fp,*ft;
char ch;
clrscr();
fp=fopen("data.txt","r");
ft=fopen("temp.txt","w");
if(fp==NULL) {
printf("cannot open one of files!");
exit();
}
while(1) {
ch=fgetc(fp);
if(ch==EOF)
break;
ch=ch+128;
fputc(ch,ft);
}
fclose(fp);
fclose(ft);
remove("data.txt");
rename("temp.txt","data.txt");
printf("task completed!");
getch();
}
while(1) {
ch=fgetc(fp);
if(ch==EOF)
break;
ch=ch-128;
/* decryption */
fputc(ch,ft);
}
fclose(fp);
fclose(ft);
remove("data.txt");
rename("temp.txt","data.txt");
printf("task completed!");
getch();
}
/* all vowels have been exchanged with first five ascii characters and
every space has been converted to 6th ascii character */
FILE *fp,*ft;
char ch;
clrscr();
fp=fopen("data.txt","r");
ft=fopen("temp.txt","w");
if(fp==NULL) {
printf("cannot open one of files!");
exit();
}
while(1) {
ch=fgetc(fp);
if(ch==EOF)
break;
encrypt(&ch);
fputc(ch,ft);
}
fclose(fp);
fclose(ft);
remove("data.txt");
rename("temp.txt","data.txt");
printf("task completed!");
getch();
}
encrypt(char *c) {
if(*c=='a') {
*c='!';
}
if(*c=='e') {
*c='@';
}
if(*c=='i') {
*c='#';
}
if(*c=='o') {
*c='$';
}
if(*c=='u') {
*c='%';
}
if(*c==' ') {
*c='^';
}
return *c;
}
while(1) {
ch=fgetc(fp);
if(ch==EOF)
break;
decrypt(&ch);
fputc(ch,ft);
}
fclose(fp);
fclose(ft);
remove("data.txt");
rename("temp.txt","data.txt");
printf("task completed!");
getch();
}
decrypt(char *c) {
if(*c=='!')
*c='a';
if(*c=='@')
*c='e';
if(*c=='#')
*c='i';
if(*c=='$')
*c='o';
if(*c=='%')
*c='u';
if(*c=='^')
*c=' ';
return *c;
}
----------------------------------------------------------------------------------------------------------------
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.
Solution:
NOTE: If on withdrawal, the balance subtracts to less than 100. The withdrawal will not be performed.
#include<stdio.h>
#include<conio.h>
void main() {
struct customer {
int accno;
char name[30];
float balance;
}cust;
struct trans {
int accno;
char trans_type;
float amount;
}tra;
FILE *fp,*ft,*ftemp;
int flag=0;
long recsize,retsize;
char another,ch;
clrscr();
fp=fopen("CUSTOMER.DAT","rb+");
if(fp==NULL) {
fp=fopen("CUSTOMER.DAT","wb+");
if(fp==NULL)
printf("cannot open customer data file!\n");
exit();
}
ft=fopen("TRANSACTIONS.DAT","rb+");
if(ft==NULL) {
ft=fopen("TRANSACTIONS.DAT","wb+");
if(ft==NULL)
printf("cannot open transactions file!\n");
exit();
}
recsize=sizeof(cust);
retsize=sizeof(tra);
while(1) {
clrscr();
printf("\t\tCutomer Transactions:\n");
printf("\t\t*********************\n\n\n");
printf("\t1: Add customer information:\n\n");
printf("\t2: Add transaction information:\n\n");
printf("\t3: List customer information:\n\n");
printf("\t4: List transaction information:\n\n");
printf("\t5: Perform transaction:\n\n");
printf("\t0: Exit:\n\n\n");
gotoxy(2,24);
printf("Your choice: ");
fflush(stdin);
ch=getche();
switch(ch) {
case '1':
clrscr();
fseek(fp,0,SEEK_END);
another='y';
while(another=='y' || another=='Y') {
printf("\t\tAdd customer information:\n");
printf("\t\t*************************\n\n");
printf("\nEnter account number: ");
scanf("%d",&cust.accno);
printf("\n\nEnter name: ");
scanf("%s",cust.name);
printf("\n\nEnter balance: ");
fflush(stdin);
scanf("%f",&cust.balance);
fwrite(&cust,recsize,1,fp);
gotoxy(2,24);
printf("Add another customer information(Y/N): ");
another=getche();
clrscr();
}
break;
case '2':
clrscr();
fseek(ft,0,SEEK_END);
another='y';
while(another=='y' || another=='Y') {
printf("\t\tAdd transaction information:\n");
printf("\t\t****************************\n\n\n");
printf("Enter existing customer account number: ");
scanf("%d",&tra.accno);
printf("\n\nEnter transaction type(D/W): ");
fflush(stdin);
scanf("%c",&tra.trans_type);
printf("\n\nEnter amount for transaction: ");
fflush(stdin);
scanf("%f",&tra.amount);
fwrite(&tra,retsize,1,ft);
gotoxy(2,24);
printf("Enter another information(Y/N): ");
another=getche();
clrscr();
}
break;
case '3':
clrscr();
printf("\t\tList customer information:\n");
printf("\t\t**************************\n\n");
rewind(fp);
while(fread(&cust,recsize,1,fp)==1) {
printf("\n%5d\t%-8s\t%5.2f\n",cust.accno,cust.name,cust.balance);
flag=1;
}
if(flag==0) {
gotoxy(2,12);
printf("No customer information found!\n");
}
printf("\n\npress any key to go back...");
getch();
break;
case '4':
clrscr();
printf("\t\tList transaction information:\n");
printf("\t\t*****************************\n\n");
rewind(ft);
while(fread(&tra,retsize,1,ft)==1) {
printf("\n%5d\t%c\t%6.2f\n",tra.accno,tra.trans_type,tra.amount);
flag=1;
}
if(flag==0) {
gotoxy(2,12);
printf("No transaction information found!\n");
}
printf("\n\npress any key to go back...");
getch();
break;
case '5':
clrscr();
printf("\t\tPerform transactions\n");
printf("\t\t********************\n\n");
rewind(fp);
while(fread(&cust,recsize,1,fp)==1) {
rewind(ft);
while(fread(&tra,retsize,1,ft)==1) {
if(cust.accno==tra.accno) {
flag=1;
if(tra.trans_type=='D' || tra.trans_type=='d') {
cust.balance+=tra.amount;
fseek(fp,-recsize,SEEK_CUR);
fwrite(&cust,recsize,1,fp);
}
else if(tra.trans_type=='W' || tra.trans_type=='w') {
if((cust.balance-tra.amount)>=100) {
cust.balance-=tra.amount;
fseek(fp,-recsize,SEEK_CUR);
fwrite(&cust,recsize,1,fp);
}
}
}
}
}
fclose(ft);
ftemp=fopen("TEMP.DAT","rb+");
if(ftemp==NULL) {
ftemp=fopen("TEMP.DAT","wb+");
}
remove("TRANSACTIONS.DAT");
rename("TEMP.DAT","TRANSACTIONS.DAT");
ft=fopen("TRANSACTIONS.DAT","rb+");
if(ft==NULL) {
ft=fopen("TRANSACTIONS.DAT","wb+");
}
if(flag==0) {
gotoxy(2,12);
printf("No active transactions\n");
}
else if(flag>0) {
gotoxy(2,12);
printf("Transactions performed seccussfully!\n");
gotoxy(2,14);
printf("NOTE: withdrawl for low balance accounts has not been performed\n");
}
gotoxy(2,24);
printf("press any key to return...");
getch();
break;
case '0':
fclose(fp);
fclose(ft);
exit();
}
}
}
---------------------------------------------------------------------------------------------------------------
{
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.
Solution:
----------------------------------------------------------------------------------------------------------------
int age;
int bld;
} b;
clrscr();
fp=fopen("BLOODBANK.DAT","wb");
if(fp==NULL) {
printf("cannot open target file!\n");
exit();
}
while(another=='Y' || another=='y') {
clrscr();
printf("\t\tInformation of Blood donor\n");
printf("\t\t**************************\n\n\n");
printf("Enter the name: ");
scanf("%s",b.name);
printf("\n\nenter the address: ");
scanf("%s",b.adr);
printf("\n\nenter the age: ");
scanf("%d",&b.age);
printf("\n\nenter the blood group(1/2/3/4): ");
scanf("%d",&b.bld);
fprintf(fp,"%s\t%s\t%d\t%d",b.name,b.adr,b.age,b.bld);
printf("\n\n\nenter more information(Y/N): ");
fflush(stdin);
another=getch();
}
fclose(fp);
}
/* This program will read the information from the file made by writing program */
/* Reading Program */
#include<stdio.h>
#include<conio.h>
void main() {
FILE *fp;
char ch;
struct blood {
char name[50];
char adr[50];
int age;
int bld;
}b;
clrscr();
fp=fopen("BLOODBANK.DAT","rb");
if(fp==NULL) {
printf("cannot open source file!\n\n");
exit();
}
while(fscanf(fp,"%s\t%s\t%d\t%d",&b.name,&b.adr,&b.age,&b.bld)!=EOF)
if(b.age<25 && b.bld==2) {
printf("\n%s\t %s\t%2d\t %d",b.name,b.adr,b.age,b.bld);
}
fclose(fp);
getch();
}
----------------------------------------------------------------------------------------------------------------
fp=fopen("NAMES.DAT","wb+");
if(fp==NULL)
printf("cannot open file! \n");
exit();
}
recsize=sizeof(s);
while(1) {
clrscr();
printf("\t\tStudent Names:\n");
printf("\t\t**************\n\n\n");
printf("\t1: Add names of students:\n\n");
printf("\t2: Search a student name:\n\n");
printf("\t3: List all student names:\n\n");
printf("\t4: List all names starting with 'S':\n\n");
printf("\t0: Exit\n\n");
gotoxy(2,24);
printf("Your choice: ");
fflush(stdin);
ch=getche();
switch(ch) {
case '1':
clrscr();
fseek(fp,0,SEEK_END);
another='y';
while(another=='y' || another=='Y') {
printf("\t\tAdd names of students:\n");
printf("\t\t**********************\n\n");
printf("Enter student number: ");
scanf("%d",&s.sn);
printf("\n\nEnter student name: ");
scanf("%s",s.name);
fwrite(&s,recsize,1,fp);
gotoxy(2,24);
printf("Enter another name(Y/N): ");
fflush(stdin);
another=getche();
clrscr();
}
break;
case '2':
clrscr();
printf("\t\tSearch a student name:\n");
printf("\t\t**********************\n\n");
printf("Enter student number: ");
scanf("%d",&num);
rewind(fp);
while(fread(&s,recsize,1,fp)==1) {
if(s.sn==num) {
printf("\n\nStudent Number: %5d\n\n",s.sn);
printf("Student name: %s\n\n",s.name);
flag=1;
break;
}
}
if(flag==0) {
printf("\n\n\nNo such name found!\n");
}
gotoxy(2,24);
printf("press any key to return...\n");
getch();
break;
case '3':
clrscr();
printf("\t\tList all student names\n");
printf("\t\t**********************\n\n\n");
rewind(fp);
while(fread(&s,recsize,1,fp)==1) {
printf("\n%5d\t%-10s\n",s.sn,s.name);
flag=1;
}
if(flag==0) {
printf("\n\n\nNo name found!\n");
}
printf("\n\n\npress any key to return...\n");
getch();
break;
case '4':
clrscr();
printf("\t\tAll name starting with 'S':\n");
printf("\t\t***************************\n\n\n");
rewind(fp);
while(fread(&s,recsize,1,fp)==1) {
if(strncmp('s',s.name[0])==0) { /* comparing only first character of \
name if it is "s" */
printf("\n%5d\t%-10s\n",s.sn,s.name);
flag=1;
}
}
if(flag==0) {
printf("\n\n\nNo name starting with \'S\' found!\n");
}
printf("\n\n\npress any key to return...\n");
getch();
break;
case '0':
fclose(fp);
exit();
}
}
}
----------------------------------------------------------------------------------------------------------------
gotoxy(20,10);
printf("Add more data(Y/N): ");
fflush(stdin);
another=getche();
}
break;
case '2':
clrscr();
gotoxy(30,2);
printf("Student data");
gotoxy(30,3);
printf("************\n\n");
rewind(fp);
while(fread(&s,sizeof(s),1,fp)==1) {
printf("\n%d\t%s\n",s.rl,s.name);
}
printf("\n\npress any key to return...");
getch();
break;
case '0':
fclose(fp);
exit();
}
}
}
int flag=0;
char ch,another;
clrscr();
fm=fopen("MASTERFILE.DAT","rb+");
if(fm==NULL) {
printf("Masterfile doesn't exist!");
}
ft=fopen("TRANSACTION.DAT","rb+");
if(ft==NULL) {
ft=fopen("TRANSACTION.DAT","wb+");
if(ft==NULL)
puts("cannot open transactions file!");
exit();
}
fu=fopen("UPDATELIST.TXT","w+");
if(fu==NULL) {
puts("cannot open target file!");
exit();
}
while(1) {
clrscr();
gotoxy(30,2);
printf("Transaction-File\n");
gotoxy(30,3);
printf("****************\n\n\n");
gotoxy(20,6);
printf("1: ADD/DELETE student data from master list: ");
gotoxy(20,8);
printf("2: Read transaction data: ");
gotoxy(20,10);
printf("3: Creat updated list: ");
gotoxy(20,12);
printf("0: Exit:");
gotoxy(2,24);
printf("Your choice: ");
fflush(stdin);
ch=getche();
switch(ch) {
case '1':
clrscr();
fseek(ft,0,SEEK_END);
another='y';
while(another=='y' || another=='Y') {
clrscr();
gotoxy(2,23);
printf("NOTE: data to be deleted should match master list");
gotoxy(2,24);
printf("NOTE: data to be added should follow the ascending nature of master list");
gotoxy(20,5);
printf("ADD/DELETE student(A/D): ");
scanf("%c",&t.stats);
gotoxy(20,7);
printf("Enter roll number: ");
scanf("%d",&t.rl);
gotoxy(20,9);
printf("Enter name: ");
fflush(stdin);
gets(t.name);
fwrite(&t,sizeof(t),1,ft);
gotoxy(20,12);
printf("Add more data(Y/N): ");
fflush(stdin);
another=getche();
}
break;
case '2':
clrscr();
gotoxy(30,2);
printf("Student data");
gotoxy(30,3);
printf("************\n\n");
rewind(ft);
while(fread(&t,sizeof(t),1,ft)==1) {
printf("\n");
if(t.stats=='a' || t.stats=='A') {
printf("ADD");
}
else {
printf("DELETE");
}
printf("\t%d\t%s\n",t.rl,t.name);
}
printf("\n\npress any key to return...");
getch();
break;
case '3':
clrscr();
gotoxy(30,2);
printf("make updated list");
gotoxy(30,3);
printf("*****************\n\n");
rewind(fm);
while(fread(&s,sizeof(s),1,fm)==1) {
flag=0;
rewind(ft);
while(fread(&t,sizeof(t),1,ft)==1) {
if(t.rl==s.rl ) {
if(t.stats=='d' || t.stats=='D') {
flag=1;
}
}
}
if(flag==0)
fprintf(fu,"\n%d\t%-s\n",s.rl,s.name);
}
rewind(ft);
while(fread(&t,sizeof(t),1,ft)==1) {
if(t.stats=='a' || t.stats=='A') {
fprintf(fu,"\n%d\t%-s\n",t.rl,t.name);
}
}
ftemp=fopen("TEMP.DAT","wb+");
fclose(ftemp);
fclose(ft);
remove("TRANSACTION.DAT");
rename("TEMP.DAT","TRANSACTION.DAT");
ft=fopen("TRANSACTION.DAT","rb+");
gotoxy(2,24);
printf("press any key to continue...");
getch();
break;
case '0':
fclose(fm);
fclose(ft);
fclose(fu);
exit();
}
}
}
-----------------------------------------------------------------------------------------------------------------
clrscr();
fseek(fp,0,SEEK_END); /* seeking cursor to reach at the end of file */
another='Y';
while(another=='Y' || another=='y') {
printf("\n\tEnter new employee information\n");
printf("\t******************************\n\n\n");
printf("\nEnter employee number: ");
scanf("%d",&e.empno);
printf("\n\nEnter employee name: ");
scanf("%s",e.name);
printf("\n\nEnter sex(M/F/O): ");
scanf(" %c",&e.sex);
printf("\n\nEnter gross salary: ");
scanf("%f",&e.gs);
/* writing new information at the end of file */
fwrite(&e,recsize,1,fp);
printf("\n\n\n\nAdd another employee(Y/N): ");
fflush(stdin);
another=getche();
clrscr();
}
break;
case '2':
clrscr();
another='Y';
while(another=='Y' || another=='y') {
printf("\n\tEnter salary information\n");
printf("\t************************\n\n");
gotoxy(2,23);
e.name[30]=e.name[30];
fseek(fp,-recsize,SEEK_CUR); /* seeking the correct location of data within\
structure in the file */
fwrite(&e,recsize,1,fp); /* writing data at correct position */
break;
}
}
if(flag==0)
/* conditional indicator used above */
printf("\n\n\n\tinformation does not exist!\n");
printf("\n\nenter another information(Y/N): ");
another=getche();
clrscr();
}
break;
case '4':
clrscr();
printf("\n\n\tDelete employee\n");
printf("\t***************\n\n");
ft=fopen("TEMP.DAT","w");
rewind(fp);
if(e.gs!=0.0 || e.gs!=0) {
file */
flag=1;
fwrite(&e,recsize,1,ft);
}
}
fclose(fp);
fclose(ft);
remove("EMP.DAT");
/* removing original file with 0 salary and renaming\
temporary without 0 salary as the original file */
rename("TEMP.DAT","EMP.DAT");
fp=fopen("EMP.DAT","rt+"); /* opening the new file, it opens because it has
not been opened before */
/* a file cannot be opened twice during
execution as you know */
if(flag>0) {
printf("\n\n\nall records with 0 gross salary have been deleted. \n");
}
gotoxy(2,24);
printf("\n\n\npress any key to return...");
getch();
break;
case '0':
fclose(fp);
exit();
case '3':
clrscr();
printf("\t\tList all employees\n");
printf("\t\t******************\n\n\n");
rewind(fp);
while(fread(&e,recsize,1,fp)==1) {
flag=1;
printf("%2d\t%6s\t%c\t%.2f\n\n",e.empno,e.name,e.sex,e.gs);
}
if(flag==0)
void replace();
void main() {
FILE *fp,*ft;
char str[80],target[80];
clrscr();
fp=fopen("FILE.TXT","r");
if(fp==NULL) {
puts("cannot open source file!");
exit();
}
ft=fopen("NEW.TXT","w");
if(ft==NULL) {
puts("cannot open target file!");
exit();
}
while(fgets(str,79,fp)!=NULL) {
replace(str,&target);
fputs(target,ft);
}
fclose(fp);
fclose(ft);
printf("\nTask completed!");
getch();
}
void replace(char *s, char *s1) {
int i=0,j=0,k=0;
char temp[100],temp2[100],main[100],*t=temp,*m=main;
/* copying to temporary string */
while(*s!='\0') {
*t=*s;
t++;
s++;
}
*t='\0';
/**********************/
/* checking each word */
/**********************/
while(temp[i]!='\0') {
temp2[j]=temp[i];
if(temp[i]==' ') {
temp2[j]='\0';
if(strcmpi(temp2,"the")==0) {
strcpy(temp2," ");
}
else if(strcmpi(temp2,"an")==0) {
strcpy(temp2," ");
}
else if(strcmpi(temp2,"a")==0) {
strcpy(temp2," ");
}
j=0;
while(temp2[j]!='\0') {
main[k]=temp2[j];
k++;
j++;
}
main[k]=' '; /* adding space after each word is copied */
k++;
j=-1;
}
i++;
j++;
}
temp2[j]='\0';
if(strcmpi(temp2,"the")==0){
strcpy(temp2," ");
}
else if(strcmpi(temp2,"an")==0) {
strcpy(temp2," ");
}
else if(strcmpi(temp2,"a")==0) {
strcpy(temp2," ");
}
/***************************/
/* last word of the string */
/***************************/
else {
j=0;
while(temp2[j]!='\0') {
main[k]=temp2[j];
k++;
j++;
}
main[k]='\0'; /* new string is completely ready */
}
while(*m!='\0') {
*s1=*m;
s1++;
m++;
}
*s1='\0';
}
-----------------------------------------------------------------------------------------------------------------
struct employee {
int empno;
char name[30];
int basic,grade;
}e;
FILE *fp;
int num=0;
long recsize;
char another,ch;
clrscr();
fp=fopen("EMPLOYEE.DAT","rb+");
if(fp==NULL) {
fp=fopen("EMPLOYEE.DAT","wb+");
if(fp==NULL)
exit();
}
recsize=sizeof(e);
while(1) {
clrscr();
printf("\t\tEmployee number database:\n");
printf("\t\t*************************\n\n");
rewind(fp);
while(fread(&e,recsize,1,fp)==1) {
printf("\n%3d\t%8s\t%5d\t%d\n",e.empno,e.name,e.basic,e.grade);
}
printf("\n\npress any key to return...\n");
getch();
break;
case '3':
clrscr();
printf("\t\tMissing employee numbers:\n");
printf("\t\t*************************\n\n");
rewind(fp);
while(fread(&e,recsize,1,fp)==1) {
num=e.empno; /* assigning the value of first employee number */
break;
}
rewind(fp); /* again rewinding the file to read from beginning */
while(fread(&e,recsize,1,fp)==1) {
if(num!=e.empno) { /* if assigned number is smaller than employee number
we will print all the number between them */
while(num<e.empno) {
printf("%4d ",num);
num++;
}
num=e.empno+1;
}
/* we will assign greater value than employee number
to make sure that both don't match until another
greater employee number is found */
else
num=e.empno+1;
}
printf("\n\n press any key to return...");
getch();
break;
case '0':
fclose(fp);
exit();
}
}
}
-----------------------------------------------------------------------------------------------------------------
FILE *fp;
char s[80];
int twd,fwd,tw=0,fw=0;
void word();
clrscr();
fp=fopen("TRIAL.TXT","r");
if(fp==NULL) {
exit();
}
while(fgets(s,79,fp)!=NULL) {
word(s,&twd,&fwd);
tw=tw+twd;
fw=fw+fwd;
}
fclose(fp);
printf("\nTotal number of words in text file = %d\n",tw);
printf("\n\nTotal number of 4 letter words = %d\n",fw);
getch();
}
void word(char ss[80],int *tw, int *fw) {
int i=0,tot_wd=0,tot_4_wd=0;
while(ss[i]!='\0') {
/************************/
/* to cound total words */
/************************/
if(ss[i]>=65 && ss[i]<=90 || ss[i]>=97 && ss[i]<=122) {
if(ss[i+1]==' ' || ss[i+1]=='.' || ss[i+1]==',' || ss[i+1]=='\n' ) {
tot_wd++;
}
}
/*********************************/
/* to count total 4 letter words */
/*********************************/
if(ss[i]==' ' || ss[i]==',' || ss[i]=='.') {
if(ss[i+1]>=65 && ss[i+1]<=90 || ss[i+1]>=97 && ss[i+1]<=122) {
if(ss[i+2]>=65 && ss[i+2]<=90 || ss[i+2]>=97 && ss[i+2]<=122) {
if(ss[i+3]>=65 && ss[i+3]<=90 || ss[i+3]>=97 && ss[i+3]<=122) {
if(ss[i+4]>=65 && ss[i+4]<=90 || ss[i+4]>=97 && ss[i+4]<=122) {
if(ss[i+5]==' ' || ss[i+5]==',' || ss[i+5]=='.' || ss[i+5]=='\n') {
tot_4_wd++;
}
}
}
}
}
}
if(ss[i]>=65 && ss[i]<=90 || ss[i]>=97 && ss[i]<=122) {
if(ss[i+1]>=65 && ss[i+1]<=90 || ss[i+1]>=97 && ss[i+1]<=122) {
if(ss[i+2]>=65 && ss[i+2]<=90 || ss[i+2]>=97 && ss[i+2]<=122) {
if(ss[i+3]>=65 && ss[i+3]<=90 || ss[i+3]>=97 && ss[i+3]<=122) {
if(ss[i+4]==' ' || ss[i+4]==',' || ss[i+4]=='.' || ss[i+4]=='\n') {
}
}
}
}
}
i++;
}
*tw=tot_wd;
*fw=tot_4_wd;
}
-----------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#define N 100
/* make a list of words, every words should be written under previous one to
make sure it's a list of words. */
/* upper case letters will be arranged before small case letter, so it is
recommended to use the list of either Capital letter or small case letters
but not both together. */
struct word {
char wrd [30];
};
void main() {
struct word w[N];
FILE *fp;
char s1[30];
int i=0,count=0;
void srt_wrd(); /* function to sort and print list */
clrscr();
fp=fopen("TRIAL.TXT","rb+");
while(fgets(s1,30,fp)!=NULL) {
strcpy(w[i].wrd,s1); /* copying each word in array of structure */
i++;
count++;
/* count of all the words */
}
printf("Total words if file = %3d\n",count);
printf("\t\tList in alphabetical order:\n");
srt_wrd(&w,count); /* function for sorting and printing list */
fclose(fp);
getch();
}
void srt_wrd( struct word *w, int n) {
int i,j,k=0;
struct word temp;
/***************************************/
/* sorting words in alphabetical order */
/***************************************/
for(i=0;i<=n;i++) {
for(j=i+1;j<n;j++) {
/* testing the first alphabets of two words */
if(w[i].wrd[0] > w[j].wrd[0]) {
temp=w[i];
w[i]=w[j];
w[j]=temp;
}
/* testing the first two alphabets of two words */
else if(w[i].wrd[0]==w[j].wrd[0] && w[i].wrd[1] > w[j].wrd[1]) {
temp=w[i];
w[i]=w[j];
w[j]=temp;
}
/* testing first three alphabets of two words */
else if(w[i].wrd[0]==w[j].wrd[0] && w[i].wrd[1]==w[j].wrd[1]) {
if(w[i].wrd[2]>w[j].wrd[2]) {
temp=w[i];
w[i]=w[j];
w[j]=temp;
}
}
/* testing first four alphabets of two words */
else if(w[i].wrd[0]==w[j].wrd[0] && w[i].wrd[1]==w[j].wrd[1]) {
if(w[i].wrd[2]==w[j].wrd[2] && w[i].wrd[3]>w[j].wrd[2]) {
temp=w[i];
w[i]=w[j];
w[j]=temp;
}
}
}
}
/*****************************/
/* printing the sorted words */
/*****************************/
for(i=0;i<n;i++) {
printf("%-s\n",w[i].wrd);
k++;
if(k==10) {
printf("\n\npress any key to go to next page...");
getch();
k=1;
clrscr();
}
}
}
--------------------------------------------------------------------------------------------------------------
clrscr();
fs=fopen("INPUT.TXT","r");
if(fs==NULL) {
printf("cannot open file!");
exit();
}
while(fgets(s,79,fs)!=NULL)
rev(s);
fclose(fs);
getch();
}
void rev(char s1[80]) {
char s2[80];
int i=0,j=0;
while(s1[i]!='\0') {
s2[j]=s1[i];
if(s1[i]==' ' || s1[i]=='\0') {
s2[j]='\0';
strrev(s2);
printf("%s ",s2);
j=-1;
}
i++;
j++;
}
s2[j]='\0';
printf("%s",strrev(s2));
}
-----------------------------------------------------------------------------------------------------------------
void print();
void main() {
FILE *fp;
char s[80];
int x=4,y=4,c=0,pg=0;
clrscr();
fp=fopen("NOTES.TXT","r");
if(fp==NULL) {
puts("cannot open file!");
exit();
}
while(fgets(s,74,fp)!=NULL) {
gotoxy(30,1);
/* printing page number */
printf("Page No: %3d",pg);
print(s,x,y,c); /* function to print */
c++;
y++;
if(c>51) {
pg++;
c=0;
gotoxy(2,24);
printf("press any key to change paper...");
getch();
clrscr();
}
if(y>22) {
gotoxy(2,24);
printf("press any key to go to next page...");
getch();
y=5;
clrscr();
}
}
fclose(fp);
}
void print(char *s,int x, int y, int c) {
/*
page border
*/
int i,bdr,bdr2;
gotoxy(1,2);
printf("%c",218);
for(bdr=3;bdr<23;bdr++) {
gotoxy(1,bdr);
printf("%c",179);
gotoxy(79,bdr);
printf("%c",179);
}
gotoxy(79,2);
printf("%c",191);
gotoxy(79,23);
printf("%c",217);
gotoxy(1,23);
printf("%c",192);
for(bdr2=2;bdr2<=78;bdr2++) {
gotoxy(bdr2,2);
printf("%c",196);
}
gotoxy(x,y);
puts(s);
if(c>50) {
for(i=2;i<79;i+=2) {
gotoxy(i,23);
printf("-");
}
}
}