Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
char nm[30],rl[10];
int i;
};
struct student d;
void main()
{
int ptr,ptr1,x,i;
long int recsize;
FILE *fp;
char b,j,w,user[10],cnf[]="qwerty",cpwd[]="qwerty",roll[10],pwd[6],another,qwe[20];
fflush(stdin);
again:
clrscr();
gotoxy(23,10);
printf("Enter UserName : ");
gets(user);
gotoxy(23,11);
printf("Enter Password : ");
fflush(stdin);
for(i=0;i<=5;i++)
{
pwd[i]=getch();
putch('*');
}
getch();
ptr=strcmp(user,cnf);
ptr1=strcmp(pwd,cpwd);
if(ptr==0&&ptr1==0)
{
repeat:
clrscr();
printf("\n********** Welcome to ADMS v 1.0 **********\n\n\n\n"
"Please select your option:\n\n\n"
"1. Take Attendance\n"
"2. Add students to the Database\n"
"3. List All the records\n"
"4. Delete All Records\n5. Exit\n\n\n"
" Please Enter your option : ");
fflush(stdin);
j=getchar();
if(j=='1')
{
clrscr();
printf("\n\nUsing this option, you can take attendance of the students whose records\nare present in the database."
"\n\n=> Press 1 to make additions.\n\n Press any other key to return to main menu.");
fflush(stdin);
b=getche();
if(b=='1')
{
recsize=sizeof(d);
if((fp=fopen("123.txt","r+"))!=NULL)
{
printf("\n\nEnter the Roll.No. :");
fflush(stdin);
scanf("%s",roll);
rewind(fp);
while(fread(&d,recsize,1,fp)==1)
{
if(strcmp(d.rl,roll)==0)
{
x=d.i;
x++;
d.i=x;
fseek(fp,-recsize,SEEK_CUR);
fwrite(&d,recsize,1,fp);
printf("\nAttendance Taken.\n\nCurrent Attendance of %s is : %d ",d.nm,d.i);
getch();
fclose(fp);
break;
}
}
goto repeat;
}
else
{
printf("\n\nFile couldn't be opened or it doesn't exists !"
"\nPlease create a new file by using option 2 from main menu.\n\nPress any key to go back to main menu.");
getch();
goto repeat;
}
}
else goto repeat;
}
else if(j=='2')
{
clrscr();
printf("\n\nUse this option to add the data\nof the students present in the database.\n\n"
"=> Press 1 to make additions.\n\n Press any other key to return to main menu.");
b=getche();
if(b=='1')
{
clrscr();
another='y';
fp=fopen("123.txt","a+");
if(fp!=NULL)
{
while(another=='y')
{
label1:
printf("\n\nEnter Roll.No : ");
fflush(stdin);
gets(d.rl);
strcpy(qwe,d.rl);
rewind(fp);
while(fread(&d,sizeof(d),1,fp)==1)
{
if(strcmp(qwe,d.rl)==0)
{
printf("\n\nRecord already exists ! Please try again.");
getch();
goto label1;
}
}
strcpy(d.rl,qwe);
fflush(stdin);
printf("Enter Name : ");
gets(d.nm);
d.i=0;
fseek(fp,sizeof(d),SEEK_END);
fwrite(&d,sizeof(d),1,fp);
printf("Record Added !\n\nAdd another record(y/n) : ");
fflush(stdin);
another=getche();
if(another=='n')
break;
}
}
else
{
printf("\n\nFile can't be opened or doesn't exists !");
getch();
goto repeat;
}
fclose(fp);
if(another=='n')
goto repeat;
}
else goto repeat;
}
else if(j=='3')
{
fflush(stdin);
if((fp=fopen("123.txt","r"))!=NULL)
{
rewind(fp);
while(fread(&d,recsize,1,fp)==1)
printf("\n\nRoll No. %s %s has attended %d lectures.",d.rl,d.nm,d.i);
getch();
fclose(fp);
goto repeat;
}
else
{
fflush(stdin);
printf("\n\nFile couldn't be opened !");
getch();
goto repeat;
}
}
else if(j=='4')
{
printf("\n\nAre you sure you want to delete the whole record ? (y/n) : ");
fflush(stdin);
w=getchar();
if(w=='y')
{
fclose(fp);
remove("123.txt");
printf("\n\nFile Deleted !!!");
getch();
goto repeat;
}
else
goto repeat;
}
else if(j=='5')
{
printf("\n\nHave a nice day !!\n\nPress any key to exit.");
getch();
exit(0);
}
else
{
printf("\n\nIncorrect option entered, press any key to try again.");
getch();
goto repeat;
}
}
else
goto again;
}
This works fine, but still it has the same problem. So, i guess i will have to download the visual c++ express for that. Right ??