i have a program to list the students , it can add new one , search and even delete a student from the list by the student number, it contains a struct of student info .all of the information is on a file named REG.DAT . my problem is in the >>DELETE<< part.i want to delete a student from the list by its student number, but when i do this i lost the rest of the list up to the person that must be deleted . please help me to solve this problem .here is my code:
void _remove()
{
int std_num = 0 , k ;
int status = 1 , i = -1 , j = 0 ;
struct students read[100] ,temp;
FILE *p ;
p = fopen("reg.dat","rb");
if(!p)
{
system("cls");
cout << ">>>>>Register file could not be opened! call the support .<<<<<" << endl;
_menu();
}
while(!feof(p))
{
fread(&read[i],sizeof(struct students),1,p) ;
i++;
}
cout << "Enter STD NO. to delete from the list : " << endl ;
cin >> std_num ;
for( j = 0 ; j < i ; j++)
{
if(std_num == read[j].std_num)
{
for( k = j + 1 ; k < i ; k++)
{
memcpy(&read[k-1] , &read[k],sizeof (students));
}
}
}
fclose(p);
FILE *fp;
fp = fopen("reg.dat","wb");
for( int l = 0 ; l < i - 2 ; l++)
fwrite(&read[l], sizeof(struct students), 1 ,fp );
fclose(fp);
system("cls") ;
cout << "STD with NO. " << std_num << " has been removed" << endl ;
_menu();
}
You should not be using
memcpyfor overlapping memory sections. You should use instead memmov. As memmov description states:Move block of memory
Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
Also:
– You could stop iterating over first loop using
breakstatement after you have found your student number.Kind regards,
Bo