我需要你的帮助来进行 c 编程中的顺序文件访问。以下代码是我编写的代码的一部分。
我可以插入一条记录,搜索一条记录,显示所有记录但是当涉及到修改和删除时,我无法获得所需的结果。在这种情况下你能指导我吗?
#include<stdio.h>
#include<stdlib.h>
int c,i,id;
char name[20];
FILE *fp;
int n;
int search(FILE *fp,int id);
void display(FILE *fp);
typedef struct details
{
int id;
char name[20];
}details;
details d;
void main()
{
printf("\nHow many records you would like to insert ? : ");
scanf("%d",&n);
fp=fopen("one.txt","a");
for(i=0;i<n;i++)
{
printf("\nEnter id and name");
scanf("%d%s",&d.id,d.name);
fwrite(&d,sizeof(d),1,fp);
}
fclose(fp);
while(1)
{
printf("\nWhat would you like to do now ? : \n");
printf("\n1.Display \t2.Search \t3.Modify \t4.Delete \t5.Exit");
scanf("%d",&c);
switch(c)
{
case 1:
fp=fopen("one.txt","r+");
display(fp);
fclose(fp);
break;
case 2:
fp=fopen("one.txt","r+");
printf("\nEnter ID to search : ");
scanf("%d",&id);
if(search(fp,id))
{
printf("\nThe record is as follows : ");
printf("\n%d\t%s",d.id,d.name);
}
else
printf("\nRecord not found");
fclose(fp);
break;
case 3:
fp=fopen("one.txt","r+");
printf("\nEnter ID to modify d record : ");
scanf("%d",&id);
if(search(fp,id))
{
printf("\nEnter new name");
scanf("%s",d.name);
fwrite(&d,sizeof(d),1,fp);
}
else
printf("\nSpecified record not found ");
fclose(fp);
break;
}
}
}
int search(FILE *fp,int id)
{
rewind(fp);
while(fread(&d,sizeof(d),1,fp))
{
if(id==d.id)
return 1;
}
return 0;
}
void display(FILE *fp)
{
rewind(fp);
while(fread(&d,sizeof(d),1,fp))
{
printf("\n%d\t%s",d.id,d.name);
}
}