- Create a fresh record.
- Removing a record
- Change a record.
- View every record.
- Exit.
Employee Information:
- Name.
- Age.
- Salary.
- Employee ID.
Approach for the program
The initial frame displays the app's name and developer details, created using printf statements and a system-supplied function . In the C/C++ standard library, the system function is available for executing commands that the OS command processor or terminal can run, providing the result once the command completes. By using System("Color 3F"), the console's background color (3) and text color (F) can be modified. When System("pause") is called, a prompt "Press any key to continue" will appear, pausing the screen. Utilizing the gotoxy function to set the data presentation coordinates can be advantageous.
Program Breakdown
struct emp {
char name [ 50 ] ;
float salary ;
int age ;
int id ;
} ;
The code mentioned above will be utilized to define the layout of an employee within the system.
void gotoxy ( int x , int y )
{
cord.X = x ;
cord.Y = y ;
SetConsoleCursorPosition (
GetStdHandle ( STD_OUTPUT_HANDLE ) ,
cord ) ;
}
FILE * fp , * ft ;
The code snippet above is responsible for defining the position of the cursor in the console.
void addrecord ( )
{
system ( " cls " ) ;
fseek ( fp , 0 , SEEK_END ) ;
char another = ' y ' ;
while ( another = = ' y ' ) {
printf ( " \ n Enter Name : " ) ;
scanf ( " % s " , e.name ) ;
printf ( " \ nEnter Age : " ) ;
scanf ( " % d " , & e.age ) ;
printf ( " \ n Enter Salary : " ) ;
scanf ( " % f " , & e.salary ) ;
printf ( " \ n Enter EMP-ID : " ) ;
scanf ( " % d " , & e.id ) ;
fwrite ( & e , size , 1 , fp ) ;
printf ( " \ n Want to add another "
" record ( Y / N ) : " ) ;
fflush ( stdin ) ;
scanf ( " % c " , & another ) ;
}
}
The above code will use to add the record.
void deleterecord ( )
{
system ( " cls " ) ;
char empname [ 50 ] ;
char another = ' y ' ;
while ( another = = ' y ' ) {
printf ( " \ n Enter employee "
" name to delete : " ) ;
scanf ( " % s " , empname ) ;
ft = fopen ( " temp.txt " , " wb " ) ;
rewind ( fp ) ;
while ( fread ( & e , size ,
1 , fp )
= = 1 ) {
if ( strcmp ( e.name ,
empname )
! = 0 )
fwrite ( & e , size , 1 , ft ) ;
}
fclose ( fp ) ;
fclose ( ft ) ;
remove ( " data.txt " ) ;
rename ( " temp.txt " , " data.txt " ) ;
fp = fopen ( " data.txt " , " rb + " ) ;
printf ( " \ n Want to delete another "
" record ( Y / N ) : " ) ;
fflush ( stdin ) ;
another = getche ( ) ;
}
}
The code snippet mentioned earlier is employed to remove a data entry from the system.
void displayrecord ( )
{
system ( " cls " ) ;
// sets pointer to start
// of the file
rewind ( fp ) ;
printf ( " \ n = = = = = = = = = = = = = = = = = = = = = = = = = "
" = = = = = = = = = = = = = = = = = = = = = = = = = = = "
" = = = = = = " ) ;
printf ( " \ n NAME \ t \ t AGE \ t \ t SALARY\ t \ t "
" \ t I D \ n " ,
e.name , e.age ,
e.salary , e.id ) ;
printf ( " = = = = = = = = = = = = = = = = = = = = = = = = = = = "
" = = = = = = = = = = = = = = = = = = = = = = = = = = = "
" = = = = \ n " ) ;
while ( fread ( & e , size , 1 , fp ) = = 1 )
printf ( " \ n % s \ t \ t % d \ t \ t%.2f \ t % 10d " ,
e.name , e.age , e.salary , e.id ) ;
printf ( " \ n \ n \ n \ t " ) ;
system ( " pause " ) ;
}
The above code will use to display records.
void modifyrecord ( )
{
system ( " cls " ) ;
char empname [ 50 ] ;
char another = ' y ' ;
while ( another = = ' y ' ) {
printf ( " \ n Enter employee name "
" to modify : " ) ;
scanf ( " % s " , empname ) ;
rewind ( fp ) ;
// While File is open
while ( fread ( & e , size , 1 , fp ) = = 1 ) {
// Compare the employee name
// with ename
if ( strcmp ( e.name , empname ) = = 0 ) {
printf ( " \ n Enter new name : " ) ;
scanf ( " % s " , e.name ) ;
printf ( " \ n Enter new age : " ) ;
scanf ( " % d " , & e.age ) ;
printf ( " \ n Enter new salary : " ) ;
scanf ( " % f " , & e.salary ) ;
printf ( " \ n Enter new EMP-ID : " ) ;
scanf ( " % d " , & e.id ) ;
fseek ( fp , -size , SEEK_CUR ) ;
fwrite ( & e , size , 1 , fp ) ;
break ;
}
}
// Ask for modifying another record
printf ( " \ n Want to modify another "
" record ( Y / N ) : " ) ;
fflush ( stdin ) ;
scanf ( " % c " , & another ) ;
}
}
The code snippet above is utilized to update the current record within the system.
Program for Employee Record System in C
// C program for the above approach
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
// Structure of the employee
struct emp {
char name[50];
float salary;
int age;
int id;
};
struct emp e;
// size of the structure
long int size = sizeof(e);
// In the start coordinates
// will be 0, 0
COORD cord = { 0, 0 };
// function to set the
// coordinates
void gotoxy(int x, int y)
{
cord.X = x;
cord.Y = y;
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE),
cord);
}
FILE *fp, *ft;
// Function to add the records
void addrecord()
{
system("cls");
fseek(fp, 0, SEEK_END);
char another = 'y';
while (another == 'y') {
printf("\nEnter Name : ");
scanf("%s", e.name);
printf("\nEnter Age : ");
scanf("%d", &e.age);
printf("\nEnter Salary : ");
scanf("%f", &e.salary);
printf("\nEnter EMP-ID : ");
scanf("%d", &e.id);
fwrite(&e, size, 1, fp);
printf("\nWant to add another"
" record (Y/N) : ");
fflush(stdin);
scanf("%c", &another);
}
}
// Function to delete the records
void deleterecord()
{
system("cls");
char empname[50];
char another = 'y';
while (another == 'y') {
printf("\nEnter employee "
"name to delete : ");
scanf("%s", empname);
ft = fopen("temp.txt", "wb");
rewind(fp);
while (fread(&e, size,
1, fp)
== 1) {
if (strcmp(e.name,
empname)
!= 0)
fwrite(&e, size, 1, ft);
}
fclose(fp);
fclose(ft);
remove("data.txt");
rename("temp.txt", "data.txt");
fp = fopen("data.txt", "rb+");
printf("\nWant to delete another"
" record (Y/N) :");
fflush(stdin);
another = getche();
}
}
// Function to display the record
void displayrecord()
{
system("cls");
// sets pointer to start
// of the file
rewind(fp);
printf("\n= = = = = = = = = = = = = = = = = = = = = = = = ="
"= = = = = = = = = = = = = = = = = = = = = = = = = = ="
"= = = = = =");
printf("\nNAME\t\tAGE\t\tSALARY\t\t"
"\tID\n",
e.name, e.age,
e.salary, e.id);
printf("= = = = = = = = = = = = = = = = = = = = = = = = = = ="
"= = = = = = = = = = = = = = = = = = = = = = = = = = ="
"= = = =\n");
while (fread(&e, size, 1, fp) == 1)
printf("\n%s\t\t%d\t\t%.2f\t%10d",
e.name, e.age, e.salary, e.id);
printf("\n\n\n\t");
system("pause");
}
// Function to modify the record
void modifyrecord()
{
system("cls");
char empname[50];
char another = 'y';
while (another == 'y') {
printf("\nEnter employee name"
" to modify : ");
scanf("%s", empname);
rewind(fp);
// While File is open
while (fread(&e, size, 1, fp) == 1) {
// Compare the employee name
// with ename
if (strcmp(e.name, empname) == 0) {
printf("\nEnter new name:");
scanf("%s", e.name);
printf("\nEnter new age :");
scanf("%d", &e.age);
printf("\nEnter new salary :");
scanf("%f", &e.salary);
printf("\nEnter new EMP-ID :");
scanf("%d", &e.id);
fseek(fp, -size, SEEK_CUR);
fwrite(&e, size, 1, fp);
break;
}
}
// Ask for modifying another record
printf("\nWant to modify another"
" record (Y/N) :");
fflush(stdin);
scanf("%c", &another);
}
}
// Driver code
int main()
{
int choice;
// opening the file
fp = fopen("data.txt", "rb+");
// showing error if file is
// unable to open.
if (fp == NULL) {
fp = fopen("data.txt", "wb+");
if (fp == NULL) {
printf("\nCannot open file...");
exit(1);
}
}
system("Color 3F");
printf("\n\n\n\n\t\t\t\t============="
"============================="
"===========");
printf("\n\t\t\t\t~~~~~~~~~~~~~~~~~~~"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~~~~~");
printf("\n\t\t\t\t==================="
"============================="
"=====");
printf("\n\t\t\t\t[|:::>:::>:::>::> "
"EMPLOYEE RECORD <::<:::<:::"
"<:::|]\t");
printf("\n\t\t\t\t==================="
"============================="
"=====");
printf("\n\t\t\t\t~~~~~~~~~~~~~~~~~~~~"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
"~~~");
printf("\n\t\t\t\t====================="
"==============================\n");
system("pause");
while (1) {
// Clearing console and asking the
// user for input
system("cls");
gotoxy(30, 10);
printf("\n1. ADD RECORD\n");
gotoxy(30, 12);
printf("\n2. DELETE RECORD\n");
gotoxy(30, 14);
printf("\n3. DISPLAY RECORDS\n");
gotoxy(30, 16);
printf("\n4. MODIFY RECORD\n");
gotoxy(30, 18);
printf("\n5. EXIT\n");
gotoxy(30, 20);
printf("\nENTER YOUR CHOICE...\n");
fflush(stdin);
scanf("%d", &choice);
// Switch Case
switch (choice) {
case 1:
// Add the records
addrecord();
break;
case 2:
// Delete the records
deleterecord();
break;
case 3:
// Display the records
displayrecord();
break;
case 4:
// Modify the records
modifyrecord();
break;
case 5:
fclose(fp);
exit(0);
break;
default:
printf("\nINVALID CHOICE...\n");
}
}
return 0;
}
Output:
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
[|:::>:::>:::>::> EMPLOYEE RECORD <::<:::<:::<:::|]
=====================================================
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
===================================================
1. ADD RECORD
2. DELETE RECORD
3. DISPLAY RECORDS
4. MODIFY RECORD
5. EXIT
ENTER YOUR CHOICE...
1
Enter Name : Ravi
Enter Age : 26
Enter Salary : 120000
Enter EMP-ID :[email protected]Want to add another record (Y/N) : Y
Enter Name : Shreya
Enter Age : 24
Enter Salary : 120000
Enter EMP-ID :[email protected]Want to add another record (Y/N) : N
1. ADD RECORD
2. DELETE RECORD
3. DISPLAY RECORDS
4. MODIFY RECORD
5. EXIT
ENTER YOUR CHOICE...
3
==========================================================
NAME AGE SALARY ID
==========================================================
Ravi 26 120000.00 0
Ravi 26 120000.00 0
Shreya 24 120000.00 0
Press any key to continue . . .
Enter employee name to delete : Ravi
Want to delete another record (Y/N) : N
4
Enter employee name to modify : Ravi
Enter new name:Ravi
Enter new age :25
Enter new salary :125000
Enter new EMP-ID:[email protected]Enter employee name to delete : Ravi
Want to delete another record (Y/N) :Y
Enter employee name to delete : Ravi
Want to delete another record (Y/N) : N