Wednesday, August 26, 2009

C Language Project

//Program to maintain students database using files
// BY PUNEET SHARMA

#include < stdio.h >
#include < conio.h >
#include < dos.h >
#include < process.h >
struct Student
{
int rollno;
char name[20];
int fee;
};
struct Student a;

void getData()
{ printf("\n\nEnter the data for the student:");
//fflush(stdin);
fflush(stdin);
puts(" \n Name : "); gets(a.name);
puts(" roll no:");
scanf("%d",&a.rollno);
printf("Fee:");
scanf("%d",&(a.fee));
}

void printData(struct Student a)
{
printf("\n%s","Name : " ); printf("%s",a.name);
printf("\n%s","Rollno : "); printf("%d",a.rollno);
printf("\n%s","Fee : "); printf("%d",a.fee);
}
void showAll()
{
FILE *fp;
char c;
clrscr();
printf("\n\n\n\n\n\t\t\t%5s|","Roll No.");
printf(" %-10s |", "Name");
printf("%6s","Fee");
printf("\n\t\t\t--------|--------------|------");
fp=fopen("student.txt","r");
rewind(fp);
while ((c=getc(fp)) != EOF)
{ fscanf(fp,"%d %s %d",&a.rollno,a.name,&a.fee);
printf("\n\t\t\t%5d | ",a.rollno);
printf("%-10s |",a.name);
printf("%6d",a.fee);
}
getche();
fclose(fp);
}
void showRec()
{
int i,z=0,j=0,n=0;
char c;
FILE *fp1;
clrscr();
z=sizeof(struct Student);
fp1=fopen("student.txt","r");
printf(" \n\n Enter the record no. you want to see : ");
scanf ("%d",&i) ;
rewind(fp1);
while(1)
{ c = getc(fp1);
if(c=='$')
j++;
if(j==i)
{
fscanf(fp1,"%d %s %d",&a.rollno,a.name,&a.fee);
printData(a);
break;
}
if(c==EOF)
break;
}
if(j!=i)
printf("\n\n\t\tSorry, Record not found in database.");
fclose(fp1);
getche();
}

void modifyRec()
{ char ch,res;
int i=0,j=0;
FILE *fp,*fp2;
clrscr();
fp=fopen("student.txt","r");
fp2=fopen("student1.txt","w");
flushall();
printf("\nEnter the record no. you want to modify: ");
scanf("%d",&i);
while(1)
{ ch = getc(fp);
if(ch=='$')
j++;
if(j==i)
{
fscanf(fp,"%d %s %d",&a.rollno,a.name,&a.fee);
printData(a);
flushall();
printf("\n\nModify this record(Y/N): ");
scanf("%c",&res);
if(res=='Y' || res=='y')
{
flushall();
ch = getc(fp);
if(ch=='$'||ch==EOF)
j++;
getData();
flushall();
flushall();
fprintf(fp2,"%c %d %s %d",'$',a.rollno,a.name,a.fee);
}
else
goto th;
}
if(ch==EOF)
break;
else
fputc(ch,fp2);
}
fclose(fp2);
fclose(fp);
fp=fopen("student.txt","w");
fp2=fopen("student1.txt","r");
ch=NULL;
while(1)
{
ch=getc(fp2);
if(ch==EOF)
break;
else
fputc(ch,fp);
}
th:
fclose(fp);
fclose(fp2);
}

void addRec()
{
FILE *fp1;
char c;
int i=0;
fp1 = fopen ("student.txt","a");
if (fp1 == NULL )
{ puts ("Internal Error: File cannot be opened." ) ;
getche();
exit(0);
}
clrscr();
printf( "\n\t%c",201);
while(i++<40)
printf( "%c",205);
printf( "%c",187);
printf ( " \n\t%c\tRoll No.: ",186 );
scanf ("%d",&a.rollno);
printf ( " \n\t\tName: " );
scanf ("%s",a.name);
printf ( " \n\t\tFee: " );
scanf ("%d",&a.fee);
fprintf(fp1,"%c",'$');
fprintf(fp1,"%d %s %d",a.rollno,a.name,a.fee);
fclose(fp1);
}
void delRec()
{
FILE *fp,*fp2;
int j=0,d,i=0;
char ch,res=NULL;
fp=fopen("student.txt","r");
fp2=fopen("student1.txt","w");
clrscr();
printf("\nEnter the record no. you want to delete: ");
scanf("%d",&i);
while(1)
{ ch = getc(fp);
if(ch=='$')
j++;
if(j==i)
{
fscanf(fp,"%d %s %d",&a.rollno,a.name,&a.fee);
printData(a);
flushall();
printf("\n\nDelete this record(Y/N): ");
scanf("%c",&res);
if(res=='Y' || res=='y')
{
flushall();
ch = getc(fp);
if(ch=='$'||ch==EOF)
j++;
flushall();
}
else
goto th;
}
if(ch==EOF)
break;
else
fputc(ch,fp2);
}
fclose(fp2);
fclose(fp);
fp=fopen("student.txt","w");
fp2=fopen("student1.txt","r");
ch=NULL;
while(1)
{
ch=getc(fp2);
if(ch==EOF)
break;
else
fputc(ch,fp);
}
th:
fclose(fp);
fclose(fp2);
}

void main()
{ char name[20];
int i=15 ; char ch;
do { clrscr();i=5;
while(i!=0){printf("\n"); i--; }
printf( "\n\t\t%c",201);i=0;
while(i++<42) printf( "%c",205);
printf( "%c",187);
printf ( " \n\t\t%c\t 1 . Show All Records %12c",186,186 );
printf ( " \n\t\t%c\t 2 . Add New Record %14c",186,186);
printf ( " \n\t\t%c\t 3 . Modify a Record %13c",186,186);
printf ( " \n\t\t%c\t 4 . Search Records %14c",186,186 );
printf ( " \n\t\t%c\t 5 . Delete Record %15c",186,186 );
printf ( "\n\t\t%c\t Esc . Exit %24c",186,186 );
printf("\n\t\t%c",200);
i=0;
while(i++<42) printf( "%c",205);
printf("%c",188);
printf ( " \n\n\n\t\t\t Enter your Choice : " );
//scanf ("%d",&i);
fflush(stdin);
ch = getch();
if (ch==27){ ch=54; }// exit(-1) ;
switch(ch-48)
{ case 1 : showAll () ; break;
case 2 : addRec () ; break;
case 3 : modifyRec() ; break;
case 4 : showRec(); break;
case 5 : delRec () ; break;
case 6 :
{printf("\n\n\t\tApplication will terminate itself in 3 seconds.");
delay(1000);printf("...3");
delay(1000);printf("..2");
delay(1000);printf(".1");
delay(500);printf(" ");
exit(0);
}
}

} while ( ((ch-48)>=1) && ( (ch-48)<=6 ));
}