C++文件的删除问题

所谓修改删除文件a某位置的内容,其实是读打开文件a,再将‘a中修改删除位置之前的内容+修改删除的内容+a中修改删除位置之后的内容’保存到文件b,关闭文件a,删除文件a,将文件b改名为与之前文件a相同的名字,仅此而已。(或者把新建文件里面的信息再重新复制会源文件)

void huizong::shanchu_zhigongxinxi()//删除职工信息
{
    system("color 8e");
    cout<<"\t\t\t请输入要删除的职工的职工号:";
    string a;
    cin>>a;
    ifstream ifs("D:\\123.text");
    if(!ifs)//打开文件失败
    {
        ifs.close();
        system("cls");
        shanchuchazhao_shibai();
    }
    else
    {
        ofstream ofs;
        char b[100];
        ofs.open("D:\\1111.text",ios::trunc);
        int falg=1;
        int ff=0;
        while(!ifs.eof())
        {
            ifs.getline(b,99,'\n');
            if(b==a)
            {
                falg=0;
                ifs.getline(b,99,'\n');
                ifs.getline(b,99,'\n');
                ifs.getline(b,99,'\n');
                ifs.getline(b,99,'\n');
                ifs.getline(b,99,'\n');
                ifs.getline(b,99,'\n');
                ifs.getline(b,99,'\n');
                ifs.getline(b,99,'\n');
            }
            else
            {
                int g=8;
                if(ff)
                    ofs<<endl;
                ff=1;
                ofs<<b;
                while(g--)
                {
                    ofs<<endl;
                    ifs.getline(b,99,'\n');
                    ofs<<b;
                }
            }
        }
        ifs.close();
        ofs.close();
        if(falg==0)
        {
            ofstream ofs;
            ofs.open("D:\\123.text",ios::trunc);
            ifstream ifs("D:\\1111.text");
            char b[100];
            int u=0;
            while(!ifs.eof())
            {
                if(u!=0)
                    ofs<<endl;
                ifs.getline(b,99,'\n');
                ofs<<b;
                u=1;
            }
            ofs.close();
            ifs.close();
            cout<<"\t\t\t删除成功\n\t\t\t继续删除请输入1,返回页面请输入2:";
            int n;
            cin>>n;
            if(n==1)
            {
                system("cls");
                shanchu_zhigongxinxi();
            }
            else
            {
                system("cls");
                yemian();
            }
        }
        else
        {
            system("cls");
            shanchuchazhao_shibai();
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值