Student record ; file handling
Code:
#include <stdio.h>#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
struct{
int roll_no;
char firstname[20];
char lastname[20];
int mark;
}student;
COORD coord = {0,0}; ///set the cordinate to 0, 0 (top-left corner of window);
void gotoxy(int x, int y){
coord.X = x; coord.Y = y; /// X and Y coordinates
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void SetColor(int ForgC)
{
WORD wColor;
///We will need this handle to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
///We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
///Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
void get_password(char* pass)
{
char temp_passP[25];
int i=0;
while(1)
{
temp_passP[i]=getch();
if(temp_passP[i]==13){break;}
else if(temp_passP[i]==8)
{
if(i!=0) {
printf("\b \b");
i--;
} else {printf("\a");}
}
else
{
printf("*");
*(pass+i) = temp_passP[i];
i++;
}
*(pass+i)='\0';
}
}
void program(){
int flag,choice,shift,rollnumber,found,continu,length; /* Main menu */
char studentname[20];
FILE *fp;
printf("-------------------------------------------------------------\n");
printf("----------------made by C code champ ------------------------\n");
printf("-------------------------------------------------------------\n");
printf("\n\tC PROGRAM OF STUDENT DATABASE SYSTEM");
Label1:
printf("\n1 -> Store a new record in database\n");
printf("2 -> Search a student record by Student First Name\n");
printf("3 -> Search a student record by rollNo\n");
printf("4 -> Quit Student Database");
printf("\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: /*choice 1*/
Label2:
printf("\nEnter Student Details:\n\nRoll number (Integer): ");
scanf("%d",&student.roll_no);
printf("\nName:");
scanf("%s",student.firstname);
printf("\nSurname:");
scanf("%s",student.lastname);
printf("\nMark(0 - 100 integer) : ");
scanf("%d",&student.mark);
fp=fopen("studentfile.txt","a+");
fprintf(fp,"\n%d\t%s\t%s\t%d\t",student.roll_no,student.firstname,student.lastname,student.mark);
fclose(fp);
printf("A student record has been added successfully...\n");
printf("\n\n1 -> Wish to add another record to database");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label2;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("Exiting.........");
break;
}
case 2: /*choice 2*/
Label4:
printf("\nEnter student first name: ");
scanf("%s",&studentname);
printf("Searching record with studentname=%s.\n",studentname);
found=0;
if((fp=fopen("studentfile.txt","r"))==NULL)
{
printf(" ! The File is Empty...\n\n");
}
else
{
while(!feof(fp)&& found==0)
{
fscanf(fp,"\n%d\t%s\t%s\t%d\t",&student.roll_no,student.firstname,student.lastname,&student.mark);
length = strlen(student.firstname);
if(student.firstname[length]==studentname[length])
found=1;
}
}
if(found)
{
printf("\nThe record is found.");
printf("\nRoll no: %d\nName: %s\nSurname: %s\nMark: %d \n",student.roll_no,student.firstname,student.lastname,student.mark);
}
else
{
printf("Not found...\n");
getch();
}
Label5:
printf("\n\n1 -> Wish to search another record");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label4;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("\nEnter a valid choice");
goto Label5;
}
case 3: /*choice 3*/
Label6:
printf("\nEnter the rollnumber: ");
scanf("%d",&rollnumber);
printf("Searching record with rollnumber=%d.\n",rollnumber);
found=0;
if((fp=fopen("studentfile.txt","r"))==NULL)
{
printf(" ! The File is Empty...\n\n");
}
else
{
while(!feof(fp)&& found==0)
{
fscanf(fp,"\n%d\t%s\t%s\t%d\t",&student.roll_no,student.firstname,student.lastname,&student.mark);
if(student.roll_no==rollnumber)
found=1;
}
}
if(found)
{
printf("\nThe record is found.");
printf("\nRoll no: %d\nName: %s\nSurname: %s\nMark: %d \n",student.roll_no,student.firstname,student.lastname,student.mark);
}
else
{
printf("Not found...\n");
getch();
}
Label7:
printf("\n\n1 -> Wish to search more..");
printf("\n2 -> Wish to move to Main Menu");
printf("\n3 -> Exit from Program\n");
scanf("%d",&shift);
if(shift==1)
goto Label6;
if(shift==2)
goto Label1;
if(shift==3)
break;
if(shift!=1&&2&&3){
printf("\nEnter a valid choice");
goto Label7;
}
case 4: break; /*choice 4*/
default :
printf(" Bad choice...Enter the choice again...\n");
goto Label1;
}
return 0;
}
int main()
{
char pass2[20],pass[20];
int x = 15, y = 16;
int grant=0,length;
FILE *f;
SetConsoleTitle("Student Record System");
SetColor(10);
gotoxy(13,9);printf(" For your own use, Please enter your personal password");
SetColor(14);
gotoxy(20,x);printf("PERSONAL PASSWORD:");
f=fopen("pass.txt","w");
gotoxy(39,x);
SetColor(12);get_password(pass2);
fprintf(f,"%s",pass2);
fclose(f);
system("cls");
enter_pass:
SetColor(10);
gotoxy(21,8);printf(" The database is password protected.");
gotoxy(21,9);printf(" Please enter you personal password");
SetColor(15);
gotoxy(24,x);printf("PASSWORD:");
gotoxy(34,x);SetColor(12);get_password(pass);
f=fopen("pass.txt","r");
while(!feof(f)&& grant==0){
fscanf(f,"%s",pass);
length = strlen(pass2);
if(pass2[length]==pass[length])
grant=1;
}
if(grant){
system("cls");
program();
}
else{
system("cls");
MessageBox(0,"YOU HAVE ENTERED THE WRONG PASSWORD! Click Ok, to enter again","Student Record System",0);
goto enter_pass;
}
fclose(f);
getch();
return 0;
}
PROBLEMS :
1.) This is our source code, the problem here is that when we are prompt to enter "personal password" we successfully got in, but it does not identify any wrong passwords.
ex. "Please enter a password"
---- password
"type your password:"
----password1 (Even if i typed the wrong password I registered earlier it still lets me into the program menu.)
I will post further problems if I find more.. ;D please do help me thank you.,.