【发布时间】:2015-11-17 18:12:56
【问题描述】:
我编写了这个 c 程序来插入、查看、修改和删除文件中的记录。文件名为 emp.dat。显示、添加和删除的代码工作正常,但修改部分不起作用。该程序要求输入详细信息以进行修改,但没有任何内容得到更新/修改。 守则:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void main()
{
FILE *outp,*inpt;
char another,choice;
struct emp
{
int emp_no,age;
char name[40];
float bs;
};
struct emp e;
char empname[40];
long int recsize;
outp=fopen("emp.dat","r+");
if(outp=='\0')
{
outp=fopen("emp.dat","w+");
if(outp=='\0')
{
puts("cannot open file\n");
exit(1);
}
}
recsize=sizeof(e);
while(1)
{
printf("1.Add records\n");
printf("2.List records\n");
printf("3.Modify records\n");
printf("4.Delete records\n");
printf("0. exit\n");
printf("Your choice\n");
fflush(stdin);
choice=getche();
switch(choice)
{
case '1': //code to add data
.
case '2': //code to display data
case '3': //code to modify data
another='Y';
while(another=='Y')
{
printf("\nEnter name of employee to modify");
scanf("%s",empname);
rewind(outp);
while(fread(&e,recsize,1,outp)==1)
{
if(strcmp(e.name,empname)==0)
{
printf("\nenter new name,age & gs");
scanf("%d %s %d %f",&e.emp_no,&e.name,&e.age,&e.bs);
fseek(outp,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,outp);
break;
}
}
printf("\nModify another record(Y/N)");
fflush(stdin);
another=getche();
}
printf("\n\n");
break;
case '4': //code to delete data
case '0':
fclose(outp);
printf("\n\n");
exit(1);
}
}
}
【问题讨论】:
-
你已经发布了相当多的代码。如果您创建一个最小、完整和可验证的示例,您可以增加有人阅读它并尝试帮助您的可能性。在这里阅读更多:stackoverflow.com/help/mcve
-
这个
if(outp=='\0')应该会抛出警告,甚至是错误。就这样做if(!outp)
标签: c file-handling