基于多态的职工管理系统(15)------清空文件

15、清空文件

功能描述:将文件中记录数据清空

 

15.1 清空函数声明

在workerManager.h中添加成员函数 void Clean_File();

    //清空文件
    void Clean_File();

15.2 清空函数实现

在workerManager.cpp中实现员函数 void Clean_File();

//清空文件
void WorkerManager::Clean_File()
{
    cout << "确认清空?" << endl;
    cout << "1、确认" << endl;
    cout << "2、返回" << endl;

    int select = 0;
    cin >> select;

    if (select == 1)
    {
        //打开模式 ios::trunc 如果存在删除文件并重新创建
        ofstream ofs(FILENAME, ios::trunc);
        ofs.close();

        if (this->m_EmpArray != NULL)
        {
            for (int i = 0; i < this->m_EmpNum; i++)
            {
                if (this->m_EmpArray[i] != NULL)
                {
                    delete this->m_EmpArray[i];
                }
            }
            this->m_EmpNum = 0;
            delete[] this->m_EmpArray;
            this->m_EmpArray = NULL;
            this->m_FileIsEmpty = true;
        }
        cout << "清空成功!" << endl;
    }

    system("pause");
    system("cls");
}

15.3 测试清空文件

在main函数分支 7 选项中,调用清空文件接口

 

测试:确认清空文件

再次查看文件中数据,记录已为空

打开文件,里面数据已确保清空,该功能需要慎用!

随着清空文件功能实现,本案例制作完毕,谢谢

posted @ 2021-06-24 17:23  清水石头  阅读(111)  评论(0)    收藏  举报