Monday, December 1, 2014

student project in c (graphics)

*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;
int chh;
int sno;
int fs,recs;
int sz;

getrec()
{
gotoxy(30,18);
cprintf("\nEnter sno          :");
scanf("%d",&s.sno);
gotoxy(30,19);
cprintf("\nEnter student name :");
scanf("%s",&s.sname);
gotoxy(30,20);
cprintf("\nEnter Per[%]       :");
scanf("%f",&s.per);
}

line(int clr,int row)
{
int x;
textcolor(clr);
gotoxy(1,row);
for(x=1;x<=80;x++)
putch(219);
}

header()
{
gotoxy(30,20);
cprintf("All student Records....");
gotoxy(30,21);
cprintf("------------------------------------------------");
gotoxy(30,22);
cprintf("S.No    Stu Name            Per.Marks");
gotoxy(30,23);
cprintf("------------------------------------------------");
}
dis()
{
cprintf("\n%-10d%-25s%-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);
gotoxy(30,47);
printf("\nRecord added....");
}

disrecs()
{
int row=23;
f=fopen("c:\\stu.dat","r");

fread((char*)&s,1,sizeof(struct student),f);
while(!feof(f))
{
gotoxy(30,row++);
dis();
fread((char*)&s,1,sizeof(struct student),f);
if(row==45)
row=23;

}
fclose(f);
}


menu()
{
clrscr();
line(2,1);
line(2,3);

textcolor(2+BLINK);
gotoxy(25,2);
cprintf("RISE GROUP OF INSTITUTIONS @ ONGOLE");

textcolor(CYAN);
gotoxy(30,5);
cprintf("\n1...Add Student Record");
gotoxy(30,6);
cprintf("\n2...Display All Record");
gotoxy(30,7);
cprintf("\n3...Search Record By Sno");
gotoxy(30,8);
cprintf("\n4...Update Record By Sno");
gotoxy(30,9);
cprintf("\n5...Delete Record By Sno");

gotoxy(30,10);
cprintf("\n0...E.X.I.T");

line(1,15);
line(2,48);

gotoxy(30,16);
textcolor(CYAN);
cprintf("\nChoice?");
putch(219);
scanf("%c",&ch);


}

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)
{
gotoxy(30,26);
cprintf("\nStudentNo : %d found",sno);
s=st;
gotoxy(30,24);
dis();
break;
}
fread((char*)&st,1,sizeof(struct student),f);
}
if(feof(f))
{
gotoxy(30,25);
cprintf("\nRecord not found...");
}

fclose(f);
}

updaterec(int sno)
{
int i;
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);
}

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");

}

main()
{
clrscr();

do
{
menu();
switch(ch)
{
case '1':
addrec();
break;
case '2':
header();
disrecs();
break;
case '3':
gotoxy(30,17);
cprintf("\nEnter Sno To Search?");
scanf("%d",&sno);
header();
searchrec(sno);
break;
case '4':
gotoxy(30,17);
cprintf("\nEnter Sno To Update?");
scanf("%d",&sno);
updaterec(sno);
break;
case '5':
gotoxy(30,17);
cprintf("\nEnter Sno To Delete?");
scanf("%d",&sno);
deleterec(sno);
break;


case '0':
exit(0);
default:
gotoxy(30,47);
perror("\nEnter choice :1/2/0 Only..");
}
getch();
}while(ch!='0');
}
















No comments:

Post a Comment