使用c程序修改文件中的记录
modify record in a file using c program
我编写了这个 c 程序来插入、查看、修改和删除文件中的记录。文件名为 emp.dat。用于显示、添加和删除的代码工作正常,但修改部分不起作用。该程序要求输入要修改的详细信息,但没有得到 updated/modified。
代码:
#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=='[=10=]')
{
outp=fopen("emp.dat","w+");
if(outp=='[=10=]')
{
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);
}
}
}
从输出中可以看出,名称并没有从 Zaid 更改为 Cow,age 和 gs
也是如此
您确实应该测试 return 值。
提示要求输入 name,age & gs
,您按要求输入。但是 scanf
被指示先获取一个整数 (emp_no
) ("%d %s %d %f"
)。缺少它立即失败并且没有任何更新。
这种情况很容易检测到:scanf
returns 成功转换的次数。
我编写了这个 c 程序来插入、查看、修改和删除文件中的记录。文件名为 emp.dat。用于显示、添加和删除的代码工作正常,但修改部分不起作用。该程序要求输入要修改的详细信息,但没有得到 updated/modified。 代码:
#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=='[=10=]')
{
outp=fopen("emp.dat","w+");
if(outp=='[=10=]')
{
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);
}
}
}
您确实应该测试 return 值。
提示要求输入 name,age & gs
,您按要求输入。但是 scanf
被指示先获取一个整数 (emp_no
) ("%d %s %d %f"
)。缺少它立即失败并且没有任何更新。
这种情况很容易检测到:scanf
returns 成功转换的次数。