Hello there! I.m trying to write a function that should delete specified entry from .dat file. But it just ADDS TWO MORE NEW ENTRIES- the same as the last entry in that file. I'm trying to read all entries into temporary file and then write them all back, except the one that should be deleted. Also I tried to write all except one into temp, but the result was the same. I just don't understand where's the problem...
Here's what I've got so far...
struct rec
{
char name[10], secname[12], studid[10], work[3];
int ex1, ex2, ex3, ex4, ex5, scolship;
float aver;
};
void delrec(int nr){
int i,j,t,big;
FILE *f;
FILE *tmp;
struct rec r;
t=0;
f=fopen("junk.dat","r");
if (!f) {
gotoxy(20,47);
textcolor(RED);
cprintf("CANNOT OPEN");
textcolor(WHITE);
getch();
goto END3;
}
big=sizeof(struct rec);
tmp=fopen("temp.dat","w");
fseek(tmp,0,SEEK_SET);
do{
fread(&r,big,1,f);
fwrite(&r,big,1,tmp);
}
while(!feof(f));
fclose(tmp);
fclose(f);
tmp=fopen("temp.dat","r");
f=fopen("junk.dat","w");
fseek(tmp,0,SEEK_SET);
fseek(f,0,SEEK_SET);
do{
if(t!=nr){
fread(&r,big,1,tmp);
fwrite(&r,big,1,f);
t++;
}
else t++;
}
while(!feof(tmp));
END3:
fclose(tmp);
fclose(f);
seefile();
getch();
}