员工管理系统的真删除实现

 先贴上代码

int use_remove()
{
    char *szNo = {0};
    char *szOrder = {0};
    pNode *thead = NULL;

    int count = 0;
    int i = 0;
    int szcount = 0;
    int flag = 0;
    int fflag = 0;
    int num = 0;

    STAFF_T *p = NULL;

    //开辟空间
    szNo = (char *)malloc(sizeof(char *));
    memset(szNo,0,sizeof(char *));

    thead = (pNode *)malloc(sizeof(pNode *));

    szOrder = (char *)malloc(sizeof(char *));
    memset(szOrder,0,sizeof(char *));
    //开辟结构体空间
    p = (STAFF_T *)malloc(sizeof(STAFF_T));

    printf("Please input the staff No.:");

    while(1)
    {
        flag = 0;
        glb_putString(szNo,10,3,1);

        szcount = sizeof(szNo)/sizeof(szNo[0]);

        //判断是否为数字
        for(i = 0; i<szcount; i++)
        {
            if(szNo[i] >= 'a' && szNo[i] <= 'z' || szNo[i] >= 'A' && szNo[i] <= 'Z')
            {
                flag = 1;
            }
        }
        if(flag == 1)
        {
            printf("\nErr:\nInput number only please:\n");
        }
        else
        {
            break;
        }
    }


    //读取文件
    head=FILE_read(staff_fp,sizeof(STAFF_T));

    count = LIST_GetCount(head);

    //判断是否为空
    if(count == 0)
    {
        printf("\nStaff No. doesn't exist ! \n");
        Sleep(1000);
        return -1;
    }

    //变脸初始化
    fflag = 0;
    num = 0;

    //数据遍历打印
    for(i = 1; i <= count; i++)
    {
        p = (STAFF_T *)LIST_GetNode(i,head);  //因为它之前是void * 所以要进行强转
        if(strcmp(p->NO,szNo)==0)
        {
            fflag = 1;
        }
    }
    if(fflag == 0)
    {
        printf("Staff No. doesn't exist ! \n");
        Sleep(1000);
        return 1;
    }


    if(count == 1)
    {
        LIST_Free(head);
        staff_fp = fopen("data/staff.txt","wb+");
        fflag = 0;
        return 1;
    }
    else
    {
        for(i = 1; i < count; i++)
        {
            p = (STAFF_T *)LIST_GetNode(i,head);  //因为它之前是void * 所以要进行强转
            if(strcmp(p->NO,szNo)== 0)
            {
                LIST_Del(i,head);
                fflag = 1;
               // printf("name : %s",p->Name);
            }
        }
    }
    if(i == count)
    {
        LIST_Del(i,head);
        staff_fp = fopen("data/staff.txt","wb+");
        //同步文件的数据
        FILE_write(staff_fp,head,sizeof(STAFF_T));
    }

    printf("\nSure to remove the staff?(y/n)\n");
    while(1)
    {
        flag = 0;
        memset(szOrder,0,sizeof(char *));
        glb_putString(szOrder,10,3,1);

        szcount = sizeof(szOrder)/sizeof(szOrder[0]);

        //判断是否为数字
        for(i = 0; i<szcount; i++)
        {
            if(szOrder[i] == 'y' || szOrder[i] == 'Y')
            {
                flag = 1;
            }
            else if(szOrder[i] == 'n' || szOrder[i] == 'N')
            {
                flag = 2;
            }
        }
        if(flag == 2)
        {
            return 1;
        }
        else if(flag == 1)
        {
            break;
        }
        printf("\nERR: \nInput 'y' or 'n' please:\n");
    }


    staff_fp = fopen("data/staff.txt","wb+");
    //同步文件的数据
    FILE_write(staff_fp,head,sizeof(STAFF_T));

    printf("\nStaff removed.\n");
    if(fflag == 0)
    {
         printf("\n Err:\n Staff No. doesn't exist !\n");
         printf("回到上级菜单\n");
    }
    getch();
}

其实我们上面代码实现真删除的思路,是先把内存里面的数据,进行删除,然后再把删除后(也就是修改后的数据),进行文件的存储。这样就可以避免对文件直接删除数据操作困难的问题。

员工管理系统代码连接:https://download.csdn.net/download/gaodes/10940115

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值