Monday, December 1, 2014

student project in -c

/*student project : by R.Venkata Subbaiah , RISE@ONGOLE */

# include <stdio.h>
# include <conio.h>
# define RECS 100


struct student
{
int sno;
char sname[15];
float per;
};
/* gloabal var */
struct student s;
FILE*f;
char ch;

getrec()
{
printf("\nEnter sno          :");
scanf("%d",&s.sno);
printf("\nEnter student name :");
scanf("%s",&s.sname);
printf("\nEnter Per[%]       :");
scanf("%f",&s.per);
}
dis()
{
printf("\n%5d%20s%15.2f",s.sno,s.sname,s.per);
}




addrec()
{

f=fopen("c:\\stu.dat","a");
getrec();
fwrite((char*)&s,1,sizeof(struct student),f);
fclose(f);
printf("\nRecord added....");
}

disrecs()
{

f=fopen("c:\\stu.dat","r");
printf("\nAll student Records....");
fread((char*)&s,1,sizeof(struct student),f);
while(!feof(f))
{
dis();
fread((char*)&s,1,sizeof(struct student),f);
}
fclose(f);
}

int searchrec(int sno)
{
struct student st;
f=fopen("c:\\stu.dat","r");
fread((char*)&st,1,sizeof(struct student),f);
while(!feof(f))
{
if(sno==st.sno)
{
printf("\nStudentNo : %d found",sno);
s=st;
dis();
break;
}
fread((char*)&st,1,sizeof(struct student),f);
}
if(feof(f))
{
printf("\nRecord not found...");
}

fclose(f);
}

updaterec(int sno)
{
int i,fs,recs,sa,chh,sz;
struct student st[RECS];
f=fopen("c:\\stu.dat","r");
/* finding records in file..*/
fseek(f,0,SEEK_END);
fs=ftell(f);
recs=fs/sizeof(struct student);
sz=recs;
/* reding all recs */
fseek(f,0,SEEK_SET);
fread((char*)&st,sizeof(struct student),recs,f);

/*
for(i=0;i<recs;i++)
printf("\n%d",st[i].sno);
*/
printf("\nFile size : %d \nRecord Size : %d\nRecords : %d",recs);

sz--;
while(sz>=0)
{

if(sno==st[sz].sno)
{
printf("\nStudentNo : %d found to update..",sno);
printf("\n1....Student no");
printf("\n2....Student name");
printf("\n3....Percentage %");
printf("\n0....E.X.I.T");
printf("\nChoice?");
scanf("%d",&chh);
switch(chh)
{
case 1:
printf("\nEnter New Sno?");
scanf("%d",&st[sz].sno);
break;
case 2:
printf("\nEnter New Stu name?");
scanf("%s",&st[sz].sname);
break;
case 3:
printf("\nEnter New Percentage?");
scanf("%f",&st[sz].per);
break;
case 0:
gotoxy(1,48);
cprintf("Return to main menu..");
return 0;
default:
gotoxy(1,48);
cprintf("Return to main menu..");
return 0;
}
break;
}
sz--;
}
fclose(f);

/* remove the file */
remove("c:\\stu.dat");
f=fopen("c:\\stu.dat","w");
fwrite((char*)&st,sizeof(struct student),recs,f);
printf("\nUpdated...");
fclose(f);
printf("\nUpdated record...");
}

deleterec(int sno)
{
struct student st;
FILE*f1; /* for writing */
f=fopen("c:\\stu.dat","r");
f1=fopen("c:\\temp.dat","w");


fread((char*)&st,sizeof(struct student),1,f);
while(!feof(f))
{
if(sno!=st.sno)
{
fwrite((char*)&st,sizeof(struct student),1,f1);
}
fread((char*)&st,sizeof(struct student),1,f);
}
fcloseall();

remove("c:\\stu.dat");
rename("c:\\temp.dat","c:\\stu.dat");
printf("\nDeleted...");

}


menu()
{
clrscr();
printf("\n\t\t\t STUDENT_MENU ");
printf("\n\t\t\t                           ");
printf("\n\t\t\t  1...Add Student Record   ");
printf("\n\t\t\t  2...Display All Record   ");
printf("\n\t\t\t  3...Search Record       ");
printf("\n\t\t\t  4...Update Record       ");
printf("\n\t\t\t  5...Delete Record       ");
printf("\n\t\t\t  0...E.X.I.T             ");
printf("\n\t\t\t                           ");
printf("\n\t\t\t \n\n");
printf("\n\t\t\t Choice?");
scanf("%c",&ch);
}

main()
{
int sno;
clrscr();

do
{
menu();
switch(ch)
{
case '1':
addrec();
break;
case '2':
disrecs();
break;
case '0':
exit(0);
case '3':
printf("\nEnter Sno To Search?");
scanf("%d",&sno);
searchrec(sno);
break;
case '4':
printf("\nEnter Sno To Update?");
scanf("%d",&sno);
updaterec(sno);
break;
case '5':
printf("\nEnter Sno To Delete?");
scanf("%d",&sno);
deleterec(sno);
break;
default:
perror("\nEnter choice :1/2/0 Only..");
}
getch();
}while(ch!='0');
}
















No comments:

Post a Comment