c++职工管理系统,我本来是能正常创建和读写文件的,运行了一次之后突然就不行了,能创建同名文件但是写不进去内容为什么,求好geigei解答,而且运行它会卡在最后一步,既不会崩溃也不会报错就一直卡着,百度了一天了阿西
#pragma once//头文件
#include
#include "worker.h"
#include
constexpr auto FileName = "empfile.txt";;
using namespace std;
class WorkerManager
{
public:
WorkerManager();
bool IsFileEmpty;
int EmpNum;
worker** Emparry;
void SystemOut();
void ShowMenu();
void ShowEmp();
void AddNum();
void save();
void init_Emp();
void DelEmp();
void ModEmp();
int GetEmpNumber();
int IsExist(int id);
~WorkerManager();
};
//WorkerManager源文件
#include "Workermanager.h"
#include "employee.h"
#include "boss.h"
#include "director.h"
void WorkerManager::ShowMenu()
{
cout << "***********************************************" << endl;
cout << "********* 欢迎使用职工管理系统 **********" << endl;
cout << "********* 0.退出软件 **********" << endl;
cout << "********* 1.增加职工信息 **********" << endl;
cout << "********* 2.显示职工信息 **********" << endl;
cout << "********* 3.删除职工信息 **********" << endl;
cout << "********* 4.修改职工信息 **********" << endl;
cout << "********* 5.查找职工信息 **********" << endl;
cout << "********* 6.按照编号排序 **********" << endl;
cout << "********* 7.清空所有文档 **********" << endl;
cout << "***********************************************" << endl;
cout << endl;
}
void WorkerManager::ModEmp()
{
if (this->IsFileEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
cout << "请输入要修改的员工编号" << endl;
int id = 0;
cin >> id;
int ret = this->IsExist(id);
if (ret != -1)
{
delete this->Emparry[ret];
int newid = 0;
string newname = "";
int select = 0;
cout << "查到:" << id << "号职工,请输入新职工号" << endl;
cin >> newid;
cout << "请输入姓名" << endl;
cin >> newname;
cout << "请选择该职工岗位" << endl;
cout << "1.普通员工" << endl;
cout << "2.老板" << endl;
cout << "3.经理" << endl;
cin >> select;
worker* worker = NULL;
switch (select)
{
case 1:
worker = new employee(id, newname, select);
break;
case 2:
worker = new boss(id, newname, select);
break;
case 3:
worker = new director(id, newname, select);
break;
default:
cout << "输入错误" << endl;
}
this->Emparry[ret] = worker;
cout << "修改成功" << endl;
this->save();
}
else
{
cout << "修改失败,查无此人" << endl;
}
}
}
void WorkerManager::DelEmp()
{
if (this->IsFileEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
cout << "请输入要删除的员工编号" << endl;
int id;
cin >> id;
this->IsExist(id);
int index = this->IsExist(id);
if (index != -1)
{
for (int i = 0; i < this->EmpNum - 1; i++)
{
this->Emparry[i] = this->Emparry[i + 1];
}
this->EmpNum--;
this->save();
cout << "删除成功" << endl;
}
else
{
cout << "删除失败,未找到该员工" << endl;
}
}
system("pause");
system("cls");
}
int WorkerManager::IsExist(int id)
{
int index = -1;
for (int i = 0; i < this->EmpNum; i++)
{
if (this->Emparry[i]->identfy == id)
{
index = i;
break;
}
}
return index;
}
void WorkerManager::ShowEmp()
{
if (this->IsFileEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
for (int i = 0; i < EmpNum; i++)
{
this->Emparry[i]->ShowInfo();
}
}
system("pause");
system("cls");
}
void WorkerManager::init_Emp()
{
ifstream ifs;
ifs.open(FileName, ios::in);
int id;
string name;
int did;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
worker* worker = NULL;
if(did == 1)
{
worker = new employee(id, name, did);
}
else if (did == 2)
{
worker = new boss(id, name, did);
}
else
{
worker = new director(id, name, did);
}
this->Emparry[index] = worker;
index++;
}
ifs.close();
}
int WorkerManager::GetEmpNumber()
{
ifstream ifs;
ifs.open(FileName, ios::in);
int id;
string name;
int did;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> did)
{
num++;
}
ifs.close();
return num;
}
WorkerManager::WorkerManager()
{
this->Emparry = new worker * [this->EmpNum];
init_Emp();
ifstream ifs;
ifs.open(FileName, ios::in);
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
this->EmpNum = 0;
this->Emparry = NULL;
this->IsFileEmpty = true;
ifs.close();
return;
}
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件夹为空" << endl;
this->EmpNum = 0;
this->Emparry = NULL;
this->IsFileEmpty = true;
ifs.close();
return;
}
int num = this->GetEmpNumber();
cout << "职工人数为: " << num << endl;
this->EmpNum = num;
this->EmpNum = 0;
this->Emparry = NULL;
}
void WorkerManager::save()
{
ofstream ofs;
ofs.open(FileName, ios::out);
for (int i = 0; i < this->EmpNum; i++)
{
ofs << this->Emparry[i]->identfy << " "
<< this->Emparry[i]->name << " "
<< this->Emparry[i]->depidentfy << endl;
}
ofs.close();
}
void WorkerManager::AddNum()
{
cout << "请增输入要增加的员工数量"<< endl;
int addnum = 0;
cin >> addnum;
if (addnum > 0)
{
int newsize = this->EmpNum + addnum;
worker** newspace = new worker * [newsize+4];
if (this->Emparry != NULL)
{
for (int i = 0; i < this->EmpNum; i++)
{
newspace[i] = this->Emparry[i];
}
}
for (int i = 0; i < addnum; i++)
{
int id;
string name;
int dselect;
cout << "请输入第" << i + 1 << "个新员工编号" << endl;
cin >> id;
cout << "请输入第" << i + 1 << "个新员工姓名" << endl;
cin >> name;
cout << "请选择该职工岗位" << endl;
cout << "1.普通员工" << endl;
cout << "2.老板" << endl;
cout << "3.经理" << endl;
cin >> dselect;
worker* worker = NULL;
switch (dselect)
{
case 1:
worker = new employee(id, name, 1);
break;
case 2:
worker = new boss(id, name, 2);
break;
case 3:
worker = new director(id, name, 3);
break;
default:
cout << "输入错误" << endl;
break;
newspace[this->EmpNum + i] = worker;
}
}
delete[]this->Emparry;
this->Emparry = newspace;
this->EmpNum = newsize;
this->save();
cout << "成功增加" << addnum << "名新员工" << endl;
}
else
{
cout << "输入有误,请重新输入" << endl;
}
system("pause");
system("cls");
}
WorkerManager::~WorkerManager()
{
if (this->Emparry != NULL)
{
delete[]this->Emparry;
this->Emparry = NULL;
}
}
void WorkerManager::SystemOut()
{
cout << "欢迎下次使用" << endl;
system("pause");
exit(0);
}
218行这个if中,如果ifs.eof()为false,则IsFileEmpty不会被初始化,ifs打开的文件不会被关闭,一直在打开状态,后面再次打开写入就会出问题。