i canot delete a record from a text file using the following function - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

i canot delete a record from a text file using the following function

shabykhanshabykhan Posts: 1Member
The RECORDS ARE PLACED IN emprec.txt IN FOLLOWING FORMAT
1,name1,gdf,dfd,df,dfd,fdfd,fdfdfsd,eree,ere,rerere,eere,rere'
'
2,name2,gdf,dfd,df,dfd,fdfd,fdfdfsd,eree,ere,rerere,eere,rere'
'
NULL

[code]
///////////////////////////DELETE RECORD\\\\\\\\\\\\\\\\


void delrec(){
clrscr();
if (recfound()==1){
printf("
Do you really want to delete this record.
");
printf("[1]Yes [2]No
");
scanf("%i",&option);
switch(option){
case 1:

if((empDbase=fopen("emprec.txt","r"))==NULL){
clrscr();
printf("


File Empty
");
}

else{
printf("Re-enter Employee no. to confirm: ");
fflush(stdin);
gets(employeeno);

if (empLookedFor!=atoi(employeeno))
{
clrscr();
printf ("
Warning! Employee no now entered does not match with previous one
");
getch();
break;
}

while(!feof(empDbase)){
fflush(stdin);
fgets (buffer,100,empDbase);
emprec[0]=strtok(buffer,",");
if(emprec[0]==NULL)
continue;
for(loop=1;loop<15;loop++){
emprec[loop]=strtok(NULL,",");
if(emprec[loop]==NULL)
break;
}



// if (feof(empDbase))
// break;

if (strcmp(emprec[0],employeeno)!=0){

if((temp=fopen("temp.txt","r"))==NULL){
temp=fopen("temp.txt","a+");

fprintf(temp,"%s,",emprec[0]);
fprintf(temp,"%s,",emprec[1]);
fprintf(temp,"%s,",emprec[2]);
fprintf(temp,"%s,",emprec[3]);
fprintf(temp,"%s,",emprec[4]);
fprintf(temp,"%s,",emprec[5]);
fprintf(temp,"%s,",emprec[6]);
fprintf(temp,"%s,",emprec[7]);
fprintf(temp,"%s,",emprec[8]);
fprintf(temp,"%s,",emprec[9]);
fprintf(temp,"%s,",emprec[10]);
fprintf(temp,"%s,",emprec[11]);
fprintf(temp,"%s
",emprec[12]); }
}
} //end while
clrscr();
printf("The record has been deleted successfully. Press any key to continue...
");
getch();
fclose(empDbase);
fclose(temp);
if (unlink("emprec.txt"))
rename("temp.txt","emprec.txt");

} //end else



// clrscr();
break;
case 2:
menu();
break;
default:
printf("
Sorry Option mismatached. Press any key to continue...
");
getch();
break;
} // end switch
}//end if
else {
printf(" Please try again");
getch();
menu();
}
}// end delrec
[/code]
Sign In or Register to comment.