Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 72 additions & 54 deletions Employee Management System-github.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,60 +256,78 @@ while(another=='y'||another=='Y')


//===================DELETING A RECORD FROM LIST ============
FILE * del(FILE * fp)
{
printHead();
printf("\n\t\t\t\Delete Employee");
Employee e;
int flag=0,tempid,siz=sizeof(e);
FILE *ft;

if((ft=fopen("temp.txt","wb+"))==NULL)//sohanuzzaman_soad
{
printf("\n\n\t\t\t\\t!!! ERROR !!!\n\t\t");
system("pause");
return fp;
}

printf("\n\n\tEnter ID number of Employee to Delete the Record");
printf("\n\n\t\t\tID No. : ");
scanf("%d",&tempid);

rewind(fp);


while((fread(&e,siz,1,fp))==1)
{//sohanuzzaman_soad
if(e.id==tempid)
{ flag=1;
printf("\n\tRecord Deleted for");
printf("\n\n\t\t%s\n\n\t\t%s\n\n\t\t%d\n\t",e.name,e.branch,e.id);
continue;
}

fwrite(&e,siz,1,ft);
}


fclose(fp);
fclose(ft);

remove("employeeInfo.txt");
rename("temp.txt","employeeInfo.txt");

if((fp=fopen("employeeInfo.txt","rb+"))==NULL)//sohanuzzaman_soad
{
printf("ERROR");
return NULL;
}

if(flag==0) printf("\n\n\t\t!!!! ERROR RECORD NOT FOUND \n\t");

printChar('-',65);
printf("\n\t");
system("pause");
return fp;
}
FILE * del(FILE * fp)
{
printHead();
printf("\n\t\t\t\\Delete Employee");
Employee e;
int flag=0,tempid,siz=sizeof(e);
FILE *ft;

/* PRECOGS_FIX: use mkstemp to create a unique, secure temporary file */
#include <unistd.h>
#include <fcntl.h>
char tmp_template[] = "tempXXXXXX";
int tmp_fd = mkstemp(tmp_template);
if(tmp_fd == -1) {
printf("\n\n\t\t\t\\t!!! ERROR creating temp file !!!\n\t\t");
system("pause");
return fp;
}

ft = fdopen(tmp_fd, "wb+");
if(!ft) {
close(tmp_fd);
unlink(tmp_template);
printf("\n\n\t\t\t\\t!!! ERROR opening temp stream !!!\n\t\t");
system("pause");
return fp;
}

printf("\n\n\tEnter ID number of Employee to Delete the Record");
printf("\n\n\t\t\tID No. : ");
if(scanf("%d",&tempid)!=1) { while(getchar()!='\n'); printf("Invalid ID\n"); fclose(ft); unlink(tmp_template); return fp; }

rewind(fp);

while((fread(&e,siz,1,fp))==1)
{
if(e.id==tempid)
{ flag=1;
printf("\n\tRecord Deleted for");
printf("\n\n\t\t%s\n\n\t\t%s\n\n\t\t%d\n\t",e.name,e.branch,e.id);
continue;
}

fwrite(&e,siz,1,ft);
}

fclose(fp);
fflush(ft);
fsync(fileno(ft));
fclose(ft);

/* atomically replace original file with the secure temporary file */
if(rename(tmp_template, "employeeInfo.txt") != 0) {
/* rename failed: cleanup and restore state */
unlink(tmp_template);
printf("ERROR replacing data file\n");
return NULL;
}

if((fp=fopen("employeeInfo.txt","rb+"))==NULL)//sohanuzzaman_soad
{
printf("ERROR");
return NULL;
}

if(flag==0) printf("\n\n\t\t!!!! ERROR RECORD NOT FOUND \n\t");

printChar('-',65);
printf("\n\t");
system("pause");
return fp;
}

//sohanuzzaman_soad
//===========MODIFY A RECORD ===========================
Expand Down