How to extract the number of structure arrays in a file? Beg the boss

The employee file management system
saves the employee data in the structure array, and then saves it in the txt file. The problem now is to add another employee data on the original basis. The following function can correctly read the number of employees, but add the first The number of the two employees' data has not changed! ! Is there a problem with the function for calculating the number of employees, or is there a problem with the function for adding employee data?

struct the_workers//职工结构数组
{
    
    
    long long num;//12位工号
    char name[100];//姓名
    char sex[10];//性别
    int birth;//生日
    int grade;//工资等级
    float salary;//工资金额
}workers[1000],*p;
int wnum()//计算职工人数
{
    
    
    int num=0;
    FILE *fp;
    fp=fopen("workers.txt","r");
    while(!feof(fp))
        if(fread(&workers[num],sizeof(struct the_workers),1,fp)==1)
            num++;
    if(num==0)
    {
    
    
        printf("无记录!\n");
        return;
    }
    fclose(fp);
    return num;
}
int addwork()//添加一个职工数据
{
    
    
    int i,num;
    num=wnum();
    p=workers;
    FILE *fp,*tp;
    fp=fopen("workers.txt","a+");
    printf("请添加职工档案信息:\n\n");
    scanf("%lld%s%s%d%d%f",&p->num,p->name,p->sex,&p->birth,&p->grade,&p->salary);
    fwrite(workers,sizeof(struct the_workers),1,fp);
    printf("添加成功!\n");
    num++;
    fclose(fp);
}

Help! !

Guess you like

Origin blog.csdn.net/weixin_46329969/article/details/104313712