gpt4 book ai didi

c - 如何从相关二进制文件中删除记录?

转载 作者:行者123 更新时间:2023-11-30 14:40:51 28 4
gpt4 key购买 nike

我正在考虑手动删除等级[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();
}

最佳答案

有两种方法可以从文件中删除记录:

1) 将要删除的记录之前的所有记录复制到一个新文件中,跳过要删除的记录,然后复制所有其余记录,然后删除原文件,然后将新文件重命名为原文件名.

2) 遍历文件直到找到要删除的记录。然后,循环将下一条记录复制到当前记录,然后按 1 条记录长度截断文件

所有这些(上面)可能非常困惑且耗时。建议:只需将要删除的记录中的字段“is”设置为0即可

关于c - 如何从相关二进制文件中删除记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55326749/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com