From 25e80909cf8074afc5db12197b7729b424f7a209 Mon Sep 17 00:00:00 2001 From: Sameer <142401625+sameer6pre@users.noreply.github.com> Date: Mon, 30 Mar 2026 19:57:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Employee-Management-System/Employee=20Ma?= =?UTF-8?q?nagement=20System-github.c=20=E2=80=94=20Precogs=20AI=20auto-fi?= =?UTF-8?q?x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Employee Management System-github.c | 126 ++++++++++++++++------------ 1 file changed, 72 insertions(+), 54 deletions(-) diff --git a/Employee Management System-github.c b/Employee Management System-github.c index 1964443..e8f9c87 100644 --- a/Employee Management System-github.c +++ b/Employee Management System-github.c @@ -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 +#include +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 ===========================