0
点赞
收藏
分享

微信扫一扫

C++ 55.基于多态的职工管理系统(11)——清空文件

功能描述:将文件中记录数据清空
1.清空函数声明
在workerManager.h中添加成员函数

//清空文件
	void Clean_File();

2.清空函数实现
在workerManager.cpp中实现成员函数

//清空文件
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 < m_EmpNum; i++)
			{
				delete this->m_EmpArray[i];
				this->m_EmpArray[i] = NULL;//每个元素置空
			}
			this->m_EmpNum = 0;

			//删除堆区数据指针
			delete[] this->m_EmpArray;
			this->m_EmpArray = NULL;
			this->m_EmpNum = 0;
			this->m_FileIsEmpty = true;
		}
		cout << "清空成功!" << endl;
		
	}
	system("pause");
	system("cls");
}

3.测试清空函数

case 7:  //清空文件
			wm.Clean_File();
			break;
举报

相关推荐

0 条评论