我正在考虑手动删除5级以下[5]的记录。我还没有尝试过任何事情,因为我不知道从哪里开始。我不希望有一个函数可以快速完成工作,我想自己实现一个功能并理解它,但是我不知道如何删除相关文件中的记录。我的代码应按以下方式工作:-二进制文件应从文本文件接收数据并显示搜索到的学生,如果该学生不存在,则应添加它。完成所有这些操作后,我应该能够删除严格低于5的成绩[5]的学生。这只是我的代码中缺少的删除功能。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
typedef struct {
int nrm; //enrolment number - works as relative key
char CNP[14]; //string ASCIIZ
char nume[30]; //string
int an; //study year
int grupa; //group
unsigned char note[20]; //grades
char is;//this is status indicator
} STUDENT;
int filesize(FILE* f, int rec_size)
{//from the crt.pos substract one and i ll know the record of...
long crt_pos;
int size;
//below compute the size of the file in records, withouth altering the crt.pozition
crt_pos = ftell(f);//save the current pozition in a var, before going to the end of the file
fseek(f, 0, SEEK_END);//go to the end of the file
size = ftell(f) / rec_size;//ftell gives the current position and the dim of the record
fseek(f, crt_pos, SEEK_SET);//ne readuce la positia curenta, initiala pe care am salvat o n var
return size;
}
void main()
{
char numefr[30] = "..\\Studenti_r_f.dat";//why \\? changes the meaning of the foll char; afiseaza un singur
FILE* f;
STUDENT x;//record variable
int i, key, dim;//key=key for the record;dim=size of file in records
//dim changes when computing the sizeof(file), at every iteration
fopen_s(&f, numefr, "wb+");
//optionally preform the file
freopen("..\\input.txt", "rt", stdin); //remove if reading data from keyboard and uncomment the printf lines
//printf("Enrollment number: ");
scanf_s("%d", &key);//read the first field, always the 1 field is the key
while (!feof(stdin))//process the key read another and so on
{
//check key range
dim = filesize(f, sizeof(STUDENT));//dim=nr de studenti din fisier;nr de recorduri;
//check the poz indicated by the key, it may have changed since the last iteration
if (key >= dim)//if the key=10, position 10 doesn t exist
{ //extend the file
x.is = 0;
fseek(f, 0, SEEK_END);//go to the end of file and write this record
for (i = 0; i < key - dim; i++)
fwrite(&x, sizeof(STUDENT), 1, f);
}
fseek(f, key * sizeof(STUDENT), SEEK_SET);//change the pos in the file, go to the pos indicated by the key
fread(&x, sizeof(STUDENT), 1, f);//read rec from file
if (x.is == 1)//if is=1, the position is taken, it must always be 0
printf("\nError: duplicate key %d. Skipping.", key);//read rest of field for student, write record from file then
else
{
x.nrm = key;
//printf_s("Name: ");
gets_s(x.nume);
gets_s(x.nume);
//printf_s("CNP: ");
gets_s(x.CNP);
//printf_s("Year: ");
scanf_s("%d", &x.an);
//printf_s("Group: ");
scanf_s("%d", &x.grupa);
for (i = 0; i < 20; i++)
//x.note[i]=0;
scanf_s("%d", &x.note[i]);
x.is = 1;
//the cur poz is after this record before we write the fseek, which brings us to the beginning of this record
fseek(f, key * sizeof(STUDENT), SEEK_SET);//ne aduce la inceputul recordului nou adaugat;
fwrite(&x, sizeof(STUDENT), 1, f);//write a record
}//everything repets with a new key
//printf_s("Enrollment number (or CTRL+Z): ");
scanf_s("%d", &key);
}
fclose(f);
fclose(stdin);
printf("\nDone. File <%s> was created. Press a key.", numefr);
_getch();
}
答案 0 :(得分:1)
有两种方法可以从文件中删除记录:
1)将要删除的记录之前的所有记录复制到新文件中,跳过要删除的记录,然后复制所有其余记录,然后删除原始文件,然后将新文件重命名为原始文件名
2)逐步浏览文件,直到找到要删除的记录。然后,在循环中将下一个记录复制到当前记录,然后将文件截短1个记录长度
所有这些(上面)可能非常麻烦且耗时。建议:只需将要删除的记录中的字段“是”设置为0