# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
char ch;
FILE*f;
/* student struct */
struct Student
{
int sno;
char sname[15];
};
line(int col,int row,int color,int width)
{
int i;
textcolor(color);
gotoxy(col,row);
for(i=1;i<=width;i++)
{
putch(219);
}
}
menu()
{
clrscr();
line(30,8,3,18);
line(30,20,3,18);
textcolor (YELLOW);
gotoxy(30,10);
cprintf("1....AddRec");
gotoxy(30,11);
cprintf("2....DisRecs");
gotoxy(30,12);
cprintf("3....Record Count");
gotoxy(30,13);
cprintf("4....Get Rec By Sno");
gotoxy(30,14);
cprintf("0....E X I T");
gotoxy(30,18);
textcolor (YELLOW+BLINK);
cprintf("Choice?");
textcolor (YELLOW);
scanf("%c",&ch);
}
addrec()
{
struct Student s;
gotoxy(30,22);
cprintf("Enter student NO?");
scanf("%d",&s.sno);
gotoxy(30,23);
cprintf("Enter student Name?");
scanf("%s",&s.sname);
/* ADDING REC */
f=fopen("c:\\student.dat","a");
if(f==NULL)
{
perror("\nUnable to open File for adding Record");
return -1;
}
fwrite((struct Student*)&s,sizeof(struct Student),1,f);
fclose(f);
gotoxy(30,24);
cprintf("Recod Added to Database.....");
}
disrecs()
{
struct Student s;
gotoxy(30,22);
cprintf("All Student Details....");
/* displaying all RECs */
f=fopen("c:\\student.dat","r");
if(f==NULL)
{
perror("\nUnable to open File for extracting Records");
return -1;
}
fread((struct Student*)&s,sizeof(struct Student),1,f);
while(!feof(f))
{
printf("\n%5d\t%15s",s.sno,s.sname);
fread((struct Student*)&s,sizeof(struct Student),1,f);
}
fclose(f);
}
fileinfo()
{
int fs,rs,recs;
gotoxy(30,22);
cprintf("Fileinformation");
f=fopen("c:\\student.dat","r");
if(f==NULL)
{
perror("\nUnable to open File for extracting Records");
return -1;
}
/* finding file size & record size*/
rs=sizeof(struct Student);
fseek(f,0,SEEK_END); /* position file poi at end */
fs=ftell(f);
recs=fs/rs;
printf("\nFile size = %d",fs);
printf("\nRec size = %d",rs);
printf("\nRecords = %d",recs);
fclose(f);
}
getrecbysno()
{
int sno;
struct Student s;
gotoxy(30,22);
cprintf("\nEnter Sno to Ge?");
scanf("%d",&sno);
f=fopen("c:\\student.dat","r");
if(f==NULL)
{
perror("\nUnable to open File for extracting Records");
return -1;
}
/* searching */
fread((struct Student*)&s,sizeof(struct Student),1,f);
while(!feof(f))
{
if(sno==s.sno)
{
printf("\n%5d\t%15s",s.sno,s.sname);
break;
}
fread((struct Student*)&s,sizeof(struct Student),1,f);
}
fclose(f);
}
main()
{
do
{
menu();
switch(ch)
{
case '1':
addrec();
break;
case '2':
disrecs();
break;
case '3':
fileinfo();
break;
case '4':
getrecbysno();
break;
case '0':
exit(0);
}
getch();
}while(ch!='0');
/* over */
}
No comments:
Post a Comment