Xóa phần tử trong 1 file như thế nào?

Đây là hàm mình nhập và lưu vào file, còn muốn xóa 1 phần tử theo maPL của nó thì như nào ạ?.

typedef struct {
	char maPL[10];
	char tenLoai[20];
}phanLoai;

void nhapPL(){
	FILE *fpl;
	phanLoai pl;
	int d;
	fpl = fopen("phanLoai.dat", "ab");
	fseek(fpl, 0, 2);
	d = ftell(fpl)/sizeof(phanLoai)+1;
	
		fflush(stdin);
		printf("Nhap ds phan loai thu %d: ", d++);
		printf("\nNhap ma phan loai: "); gets(pl.maPL);
		printf("Nhap ten loai: "); gets(pl.tenLoai);
		
		fwrite(&pl, sizeof(phanLoai), 1, fpl);
		printf("\nNhap bat ki de tiep tuc");
		getch();
				
	
	fclose(fpl);	
}

int main(){
nhapPL();
}

Cách thông thường thì bạn fread() -> array kiểu phanLoai.
Bài toán trở về xóa phần tử vào ghi vào file.
Có thể sử dụng file temporary cho dễ nhé bạn

3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?