增加职工操作是正确的,但删除职工有错误,望大侠指点
#include<stdio.h> #include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#define STACK_INIT_SIZE 10
#define OK 1
#define N 5
#define TRUE 1
#define FALSE 0
#define ERROR 0
struct employ /*定义职工类型*/
{
int employnum;
char employname[15];
int employage;
char employsex[2];
long int employtage;
}memploy[50];
addemploy()
{
FILE *fp1;
char choice='1';
int i=1;
fp1=fopen("employ.txt","a+");
while(choice=='1')
{
printf("请你输入职工号码\n");
scanf("%d",&memploy[i].employnum);
printf("请你输入职工名\n");
scanf("%s",memploy[i].employname);
printf("请输入职工年龄\n");
scanf("%d",&memploy[i].employage);
printf("请你输入性别\n");
scanf("%s",memploy[i].employsex);
printf("请输入职工的工资\n");
scanf("%d",&memploy[i].employtage);
fprintf(fp1,"%d %s %d %s %d\n",memploy[i].employnum,memploy[i].employname,
memploy[i].employage,memploy[i].employsex,memploy[i].employtage);
printf("是否要输入下个职工信息?\n");
scanf("%s",&choice);
}
printf("按任意键返回\n");
fclose(fp1);
}
delemploy()
{
FILE *fp,*fp2;
int i;
char employname[10],choice;
fp=fopen("employ.txt","r");
fp2=fopen("bookl.txt","w");
printf("请输入你要删除的职工名\n");
printf("如果你输入的职工存在,系统自动删除该信息!如果不存在,系统不做任何改动\n");
scanf("%s",employname);
for(i=0;fread(&memploy[i],sizeof(struct employ),1,fp)!=0;i++)
{
if(strcmp(employname,memploy[i].employname)!=0)
{
fprintf(fp2,"%d %s %d %s %d\n",memploy[i].employnum,memploy[i].employname,
memploy[i].employage,memploy[i].employsex,memploy[i].employtage);
}
}
fclose(fp);
fclose(fp2);
printf("是否真的要删除该职工信息?删除后的所有信息将无法恢复《Y/N》\n");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
fp=fopen("employ.txt","w");
fp2=fopen("bookl.txt","r");
for(i=0;fread(&memploy[i],sizeof(struct employ),1,fp2)!=0;i++)
{
fprintf(fp,"%d %s %d %s %d\n",memploy[i].employnum,memploy[i].employname,
memploy[i].employage,memploy[i].employsex,memploy[i].employtage);
}
fclose(fp);
fclose(fp2);
fp2=fopen("bookl.txt","w");
fclose(fp2);
printf("按任意键返回\n");
getch();
return TRUE;
}
else
{
printf("按任意键返回\n");
getch();
return FALSE;
}
}
void main()
{
addemploy();
delemploy();
}
}