Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in this c code please add this user system in log in system badly need this please username 1 : monjur pass : 1234 username

in this c code please add this user system in log in system badly need this please
username 1 : monjur
pass : 1234
username 2 : diponkar
pass: 1234
username 3: sanjida
pass: 1234
username 4: ahnaf
pass:1234
#include
#include
#include
#include
#define MAX_YR 9999
#define MIN_YR 1900
#define MAX_PASSWORD 30
#define FILE_NAME "studentRecordSystem.txt"
#define MAX_NAME 50
#define MAX_ADDRESS 300
#define FILE_HEADER_SIZE sizeof(sFileHeader)
typedef struct
{
char username[MAX_NAME];
char password[MAX_PASSWORD];
} sFileHeader;
typedef struct
{
int roll;
char studentName[MAX_NAME];
char fatherName[MAX_NAME];
int clss;
int marks;
} s_StudentInfo;
void printMessage(const char* message)
{
int len =0;
int pos = 0;
len = (78 - strlen(message))/2;
printf("\t\t\t");
for(pos =0 ; pos < len ; pos++)
{
printf(" ");
}
printf("%s",message);
}
void FrontPage(const char *message)
{
system("cls");
printf("\t\t\t<=========================================================================>");
printf(" \t <><><><><>||||||||||||---------------------------------------------------||||||||||||<><><><><>");
printf(" \t <||||||||||||||||||||||| Student Record Management System |||||||||||||||||||||||>");
printf(" \t <><><><><>||||||||||||---------------------------------------------------||||||||||||<><><><><>");
printf(" \t\t\t<=========================================================================> ");
printMessage(message);
printf(" \t\t\tOOO======================================================================OOO");
}
void welcomeMessage()
{
FrontPage("WELCOME");
printf(" ");
printf(" \t\t\t\t ========================================================");
printf(" \t\t\t\t <>|==========================================|<>");
printf(" \t\t\t\t <><>| STUDENT |<><>");
printf(" \t\t\t\t <><><>| MANAGEMENT |<><><>");
printf(" \t\t\t\t<><><><>| SYSTEM |<><><><>");
printf(" \t\t\t\t <><><>| BY |<><><>");
printf(" \t\t\t\t <><>| Monjur Diponkar Sanjida & Ahnaf |<><>");
printf(" \t\t\t\t <>|==========================================|<>");
printf(" \t\t\t\t ======================================================== ");
printf(" \t\t\t\t Press any key to continue.....");
getch();
}
void addStudent()
{
s_StudentInfo addStudentIn = {0};
FILE *fp = NULL;
int chk = 0;
fp = fopen(FILE_NAME,"a+");
if( fp == NULL )
{
printf("File is not opened ");
exit(1);
}
FrontPage("ADD NEW STUDENT");
printf(" \t\t\tENTER DETAILS BELOW:");
printf(" \t\t\t=========================================================================== ");
do
{
printf(" \t\t\tName = ");
fflush(stdin);
fgets(addStudentIn.studentName,MAX_NAME,stdin);
chk = isNameValid(addStudentIn.studentName);
if (!chk )
{
printf(" \t\t\tName contain invalid character. Please enter again.");
}
}
while(!chk );
printf(" \t\t\tRoll No = ");
fflush(stdin);
scanf("%d",&addStudentIn.roll);
do
{
printf(" \t\t\tFather Name = ");
fflush(stdin);
fgets(addStudentIn.fatherName,MAX_NAME,stdin);
chk = isNameValid(addStudentIn.fatherName);
if (!chk)
{
printf(" \t\t\tName contain invalid character. Please enter again.");
}
}
while(!chk );
do
{
printf(" \t\t\tClass = ");
fflush(stdin);
scanf("%d",&addStudentIn.clss);
chk = isClassValid(addStudentIn.clss);
if (!chk )
{
printf(" \t\t\tClass input invalid. Please enter again.");
}
}
while(!chk );
do
{
printf(" \t\t\tMarks in percentage = ");
fflush(stdin);
scanf("%d",&addStudentIn.marks);
chk = isMarksValid(addStudentIn.marks);
if (!chk )
{
printf(" \t\t\tMarks input invalid. Please enter again.");
}
}
while(!chk );
fwrite(&addStudentIn,sizeof(addStudentIn), 1, fp);
fclose(fp);
}
void searchStudentByRoll()
{
char SSName[MAX_NAME];
int found = 0;
int RollNo =0;
s_StudentInfo addStudentIn = {0};
FILE *fp = NULL;
fp = fopen(FILE_NAME,"r");
if(fp == NULL)
{
printf(" \t\t\tFile is not opened ");
exit(1);
}
FrontPage("SEARCH STUDENTS");
if (fseek(fp,FILE_HEADER_SIZE,SEEK_SET) != 0)
{
fclose(fp);
printf(" \t\t\tFacing issue while reading file ");
exit(1);
}
printf(" \t\t\tEnter ROLL NO to search:");
fflush(stdin);
scanf("%d",&RollNo);
while (fread (&addStudentIn, sizeof(addStudentIn), 1, fp))
{
if(addStudentIn.roll == RollNo)
{
found = 1;
break;
}
}
if(found)
{
printf(" \t\t\tRoll No = %d",addStudentIn.roll);
printf(" \t\t\tName = %s",addStudentIn.studentName);
printf("\t\t\tFather Name = %s",addStudentIn.fatherName);
printf("\t\t\tClass = %d",addStudentIn.clss);
printf("\t\t\tMarks in Percentage = %d",addStudentIn.marks);
printf(" \t\t\t=========================================================================== ");
printf(" ");
}
else
{
printf(" \t\t\tNo Record");
}
fclose(fp);
printf(" \t\t\tPress any key to go to main menu.....");
fflush(stdin);
getchar();
}
void searchStudentByName()
{
int found = 0;
char SSName[MAX_NAME];
s_StudentInfo addStudentIn = {0};
FILE *fp = NULL;
fp = fopen(FILE_NAME,"r");
if(fp == NULL)
{
printf(" \t\t\tFile is not opened ");
exit(1);
}
FrontPage("SEARCH STUDENTS");
if (fseek(fp,FILE_HEADER_SIZE,SEEK_SET) != 0)
{
fclose(fp);
printf(" \t\t\tFacing issue while reading file ");
exit(1);
}
printf(" \t\t\tEnter NAME to search:");
fflush(stdin);
fgets(SSName,MAX_NAME,stdin);
while (fread (&addStudentIn, sizeof(addStudentIn), 1, fp))
{
if( !strcmp(addStudentIn.studentName,SSName) )
{
found = 1;
break;
}
}
if(found)
{
printf(" \t\t\tRoll No = %d",addStudentIn.roll);
printf(" \t\t\tName = %s",addStudentIn.studentName);
printf("\t\t\tFather Name = %s",addStudentIn.fatherName);
printf("\t\t\tClass = %d",addStudentIn.clss);
printf("\t\t\tMarks in Percentage = %d",addStudentIn.marks);
printf(" \t\t\t=========================================================================== ");
printf(" ");
}
else
{
printf(" \t\t\tNo Record");
}
fclose(fp);
printf(" \t\t\tPress any key to go to main menu.....");
fflush(stdin);
getchar();
}
void viewStudent()
{
int found = 0;
s_StudentInfo addStudentIn = {0};
FILE *fp = NULL;
unsigned int countStudent = 1;
FrontPage("VIEW STUDENT DETAILS");
fp = fopen(FILE_NAME,"r");
if(fp == NULL)
{
printf("File is not opened ");
exit(1);
}
if (fseek(fp,FILE_HEADER_SIZE,SEEK_SET) != 0)
{
fclose(fp);
printf("Facing issue while reading file ");
exit(1);
}
while (fread (&addStudentIn, sizeof(addStudentIn), 1, fp))
{
printf(" \t\t\tRoll No = %d",addStudentIn.roll);
printf(" \t\t\tName = %s",addStudentIn.studentName);
printf("\t\t\tFather Name = %s",addStudentIn.fatherName);
printf("\t\t\tClass = %d",addStudentIn.clss);
printf("\t\t\tMarks in Percentage = %d",addStudentIn.marks);
printf(" \t\t\t=========================================================================== ");
printf(" ");
found = 1;
++countStudent;
}
fclose(fp);
if(!found)
{
printf(" \t\t\tNo Record");
}
printf(" \t\t\tPress any key to go to main menu.....");
fflush(stdin);
getchar();
}
void deleteStudent()
{
char SSName[MAX_NAME];
int found = 0;
int studentDelete = 0;
sFileHeader fileHeaderInfo = {0};
s_StudentInfo StudentInfo = {0};
FILE *fp = NULL;
FILE *tmpFp = NULL;
FrontPage("Delete Student Details");
fp = fopen(FILE_NAME,"r");
if(fp == NULL)
{
printf("File is not opened ");
exit(1);
}
tmpFp = fopen("tmp.txt","w");
if(tmpFp == NULL)
{
fclose(fp);
printf("File is not opened ");
exit(1);
}
fread (&fileHeaderInfo,FILE_HEADER_SIZE, 1, fp);
fwrite(&fileHeaderInfo,FILE_HEADER_SIZE, 1, tmpFp);
printf(" \t\t\tEnter Roll NO. of the student to be deleted:");
scanf("%d",&studentDelete);
fflush(stdin);
printf(" \t\t\tEnter NAME of the student to be deleted:");
fflush(stdin);
fgets(SSName,MAX_NAME,stdin);
while (fread (&StudentInfo, sizeof(StudentInfo ), 1, fp))
{
if(StudentInfo.roll != studentDelete && strcmp(StudentInfo.studentName,SSName))
{
fwrite(&StudentInfo,sizeof(StudentInfo), 1, tmpFp);
}
else
{
found = 1;
}
}
(found)? printf(" \t\t\tRecord deleted successfully....."):printf(" \t\t\tRecord not found");
fclose(fp);
fclose(tmpFp);
remove(FILE_NAME);
rename("tmp.txt",FILE_NAME);
printf(" \t\t\tPress any key to continue.....");
fflush(stdin);
getchar();
}
void updatePass(void)
{
sFileHeader fileHeaderInfo = {0};
FILE *fp = NULL;
char userName[MAX_NAME] = {0};
char password[MAX_PASSWORD] = {0};
FrontPage("Update Login Info");
fp = fopen(FILE_NAME,"r+");
if(fp == NULL)
{
printf("File is not opened ");
exit(1);
}
fread (&fileHeaderInfo,FILE_HEADER_SIZE, 1, fp);
if (fseek(fp,0,SEEK_SET) != 0)
{
fclose(fp);
printf(" \t\t\tFacing issue while updating password ");
exit(1);
}
printf(" \t\t\tNew Username:");
fflush(stdin);
fgets(userName,MAX_NAME,stdin);
printf(" \t\t\tNew Password:");
fflush(stdin);
fgets(password,MAX_PASSWORD,stdin);
strncpy(fileHeaderInfo.username,userName,sizeof(userName));
strncpy(fileHeaderInfo.password,password,sizeof(password));
fwrite(&fileHeaderInfo,FILE_HEADER_SIZE, 1, fp);
fclose(fp);
printf(" \t\t\tYour Password has been changed successfully");
printf(" \t\t\tPress Any key to Exit");
fflush(stdin);
getchar();
exit(1);
}
void editADD()
{
s_StudentInfo addStudentIn = {0};
FILE *fp = NULL;
int chk = 0;
fp = fopen(FILE_NAME,"ab+");
if( fp == NULL )
{
printf("File is not opened ");
exit(1);
}
printf(" \t\t\tENTER EDITED DETAILS BELOW:");
do
{
printf(" \t\t\tName = ");
fflush(stdin);
fgets(addStudentIn.studentName,MAX_NAME,stdin);
chk = isNameValid(addStudentIn.studentName);
if (!chk )
{
printf(" \t\t\tName contain invalid character. Please enter again.");
}
}
while(!chk );
printf(" \t\t\tRoll No = ");
fflush(stdin);
scanf("%d",&addStudentIn.roll);
do
{
printf(" \t\t\tFather Name = ");
fflush(stdin);
fgets(addStudentIn.fatherName,MAX_NAME,stdin);
chk = isNameValid(addStudentIn.fatherName);
if (!chk)
{
printf(" \t\t\tName contain invalid character. Please enter again.");
}
}
while(!chk );
do
{
printf(" \t\t\tClass = ");
fflush(stdin);
scanf("%d",&addStudentIn.clss);
chk = isClassValid(addStudentIn.clss);
if (!chk )
{
printf(" \t\t\tClass input invalid. Please enter again.");
}
}
while(!chk );
do
{
printf(" \t\t\tMarks in percentage = ");
fflush(stdin);
scanf("%d",&addStudentIn.marks);
chk = isMarksValid(addStudentIn.marks);
if (!chk )
{
printf(" \t\t\tMarks input invalid. Please enter again.");
}
}
while(!chk );
fwrite(&addStudentIn,sizeof(addStudentIn), 1, fp);
fclose(fp);
}
void editdelete( int studentDelete )
{
int found = 0;
sFileHeader fileHeaderInfo = {0};
s_StudentInfo StudentInfo = {0};
FILE *fp = NULL;
FILE *tmpFp = NULL;
fp = fopen(FILE_NAME,"r");
if(fp == NULL)
{
printf("File is not opened ");
exit(1);
}
tmpFp = fopen("tmp.txt","w");
if(tmpFp == NULL)
{
fclose(fp);
printf("File is not opened ");
exit(1);
}
fread (&fileHeaderInfo,FILE_HEADER_SIZE, 1, fp);
fwrite(&fileHeaderInfo,FILE_HEADER_SIZE, 1, tmpFp);
while (fread (&StudentInfo, sizeof(StudentInfo ), 1, fp))
{
if(StudentInfo.roll != studentDelete)
{
fwrite(&StudentInfo,sizeof(StudentInfo), 1, tmpFp);
}
else
{
found = 1;
}
}
// (found)? printf(" \t\t\tRecord deleted successfully....."):printf(" \t\t\tRecord not found");
fclose(fp);
fclose(tmpFp);
remove(FILE_NAME);
rename("tmp.txt",FILE_NAME);
editADD( );
}
void editStudent()
{
int found = 0;
int RollNo =0;
s_StudentInfo addStudentIn = {0};
FILE *fp = NULL;
fp = fopen(FILE_NAME,"r");
if(fp == NULL)
{
printf(" \t\t\tFile is not opened ");
exit(1);
}
FrontPage("EDIT STUDENT INFO");
if (fseek(fp,FILE_HEADER_SIZE,SEEK_SET) != 0)
{
fclose(fp);
printf(" \t\t\tFacing issue while reading file ");
exit(1);
}
printf(" \t\t\tEnter ROLL NO to search:");
fflush(stdin);
scanf("%d",&RollNo);
while (fread (&addStudentIn, sizeof(addStudentIn), 1, fp))
{
if(addStudentIn.roll == RollNo )
{
found = 1;
break;
}
}
if(found)
{
printf(" \t\t\tRoll No = %d",addStudentIn.roll);
printf(" \t\t\tName = %s",addStudentIn.studentName);
printf("\t\t\tFather Name = %s",addStudentIn.fatherName);
printf("\t\t\tClass = %d",addStudentIn.clss);
printf("\t\t\tMarks in Percentage = %d",addStudentIn.marks);
fclose(fp);
editdelete( RollNo );
}
else
{
printf(" \t\t\tNo Record Found! \t\t\tEnter any key to go back to Main Menu.......");
fclose(fp);
fflush(stdin);
getchar();
}
}
void menu()
{
system("cls");
int x = 0;
do
{
FrontPage("MAIN MENU");
printf(" \t\t\tPress 1 to Add Student");
printf(" \t\t\tPress 2 to Search Student");
//printf(" \t\t\tPress 3 to Search Student by Name");
printf(" \t\t\tPress 3 to View All Student Info");
printf(" \t\t\tPress 4 to Edit Any Student Info");
printf(" \t\t\tPress 5 to Delete Any Student Info");
printf(" \t\t\tPress 6 to Update Password Info");
printf(" \t\t\tPress 0 to Exit");
printf(" \t\t\tSelect Any Option: ");
printf("\t\t\t");
scanf("%d",&x);
switch(x)
{
case 1:
addStudent();
break;
case 2:
{
printf(" \t\t\t Press 1 to search by Name");
printf(" \t\t\t Press 2 to search by Roll");
printf(" \t\t\tSelect Any Option: ");
printf("\t\t\t");
int p;
scanf("%d",&p);
switch(p)
{
case 1:
searchStudentByName();
break;
case 2:
searchStudentByRoll();
break;
default:
printf("Choose right option please!");
}
}
case 3:
viewStudent();
break;
case 4:
editStudent();
break;
case 5:
deleteStudent();
break;
case 6:
updatePass();
break;
case 0:
system("cls");
printf(" \t\t\t\t\t\t>>>>>>SHUTING DOWN<<<<<< ");
exit(1);
break;
default:
printf(" \t\t\tINVALID INPUT");
}
}
while( x != 0 );
}
int isMarksValid( int mrk )
{
if( mrk >= 0 && mrk <= 100 )
{
return 1;
}
else
{
return 0;
}
}
int isClassValid( int cls )
{
if( cls > 0 && cls < 12 )
{
return 1;
}
else
{
return 0;
}
}
int isNameValid(const char *name)
{
int validName = 1;
int len = 0;
int index = 0;
len = strlen(name);
for(index =0; index
{
if(!(isalpha(name[index])) && (name[index] != ' ') && (name[index] != ' '))
{
validName = 0;
break;
}
}
return validName;
}
int isFileExists( const char *path )
{
FILE *fp = fopen(path,"r");
int statusFlag = 0;
if( fp != NULL )
{
statusFlag = 1;
fclose(fp);
}
return statusFlag;
}
void login()
{
int i=0;
char ch;
char userName[MAX_NAME] = {0};
char password[MAX_PASSWORD] = {0};
int L = 0;
sFileHeader fileHeaderInfo = {0};
FILE *fp = NULL;
FrontPage("Login");
fp = fopen(FILE_NAME,"r");
if(fp == NULL)
{
printf("File is not opened ");
exit(1);
}
fread (&fileHeaderInfo,FILE_HEADER_SIZE, 1, fp);
fclose(fp);
do
{
printf(" \t\t\t\tUsername:");
//fgets(userName,MAX_NAME,stdin);
gets(userName);
printf(" \t\t\t\tPassword:");
while((ch=getch())!=13)
{
password[i]=ch;
i++;
printf("*");
}
password[i]='\0';
if((!strcmp(userName,fileHeaderInfo.username)) && (!strcmp(password,fileHeaderInfo.password)))
{
menu();
}
else
{
i=0;
printf(" \t\t\t\tLogin Failed Enter Again Username & Password ");
L++;
}
}
while(L<=3);
if(L>3)
{
FrontPage("Login Failed");
printf("\t\t\t\tSorry,Unknown User.");
getch();
system("cls");
}
}
void settingUp()
{
FILE *fp = NULL;
int statusFlag = 0;
const char byDefaultUsername[] = "bony";
const char byDefaultPassword[] = "1234";
sFileHeader fileHeaderInfo = {0};
statusFlag = isFileExists(FILE_NAME);
if( !statusFlag )
{
fp = fopen(FILE_NAME,"w");
if( fp != NULL )
{
strncpy(fileHeaderInfo.password, byDefaultPassword, sizeof(byDefaultPassword));
strncpy(fileHeaderInfo.username, byDefaultUsername, sizeof(byDefaultUsername));
fwrite(&fileHeaderInfo,FILE_HEADER_SIZE, 1, fp);
fclose(fp);
}
}
}
int main()
{
settingUp();
welcomeMessage();
login();
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Students also viewed these Databases questions

Question

6. Does your speech have a clear and logical structure?

Answered: 1 week ago