《黑马程序员视频学C++》中的练手项目,这是没看视频先自己实现的版本,尚未实现按编号查找的功能,并且输入验证之类的细节也没添加,就是个初始版本。
系统功能描述:
职工分为三类,普通职工、经理和老板,各自有各自的职责。
实现下图功能,并将信息存入本地文本文件。
一、功能展示
1、系统初始页面:

2、增加和展示职工信息:

对应文本文件:

3、删除员工信息:

4、修改职工信息:

对应文本文件:

5、查找职工信息:

6、清空所有信息:

文本文件也跟着清除。
7、退出系统:

二、代码
#include<iostream>
#include<fstream>
#include<string>
#include<cstdio>
using namespace std;
int locate(int id);
//岗位数字1:普通员工 2:经理 3:老板
class Worker {
public:
Worker(int id,string name,int post) {
w_id = id;
w_name = name;
w_post = post;
}
private:
int w_id;
string w_name;
int w_post;//岗位
};
class Manager {
public:
Manager(int id,string name,int post) {
m_id = id;
m_name = name;
m_post = post;
}
private:
int m_id;
string m_name;
int m_post;//岗位
};
class Boss {
public:
Boss(int id,string name,int post) {
b_id = id;
b_name = name;
b_post = post;
}
private:
int b_id;
string b_name;
int b_post;//岗位
};
void insert_inf() {//插入职工信息函数
int ins_id;
string ins_name;
int ins_post;
ofstream ofs;
ofs.open("information.txt", ios::out|ios::app);
cout << "职工编号:";//编号不能重复,待加入重复验证
cin >> ins_id;
cout << "职工姓名:";
cin >> ins_name;
cout << "职工岗位:";
cin >> ins_post;
if (ins_post == 1) {
new Worker(ins_id,ins_name,ins_post);
ofs << "职工编号:" << ins_id << "\t职工姓名:" << ins_name << "\t职工岗位:职工 " << "\t职责:完成经理交给的任务" <<endl;
}
if (ins_post == 2) {
new Manager(ins_id, ins_name, ins_post);
ofs << "职工编号:" << ins_id << "\t职工姓名:" << ins_name << "\t职工岗位:经理 " << "\t职责:完成老板交给的任务并下发任务给员工" << endl;
}
if (ins_post == 3) {
new Boss(ins_id, ins_name, ins_post);
ofs << "职工编号:" << ins_id << "\t职工姓名:" << ins_name << "\t职工岗位:老板 " << "\t职责:管理公司所有事务" << endl;
}
ofs.close();
}
void display_inf() {//展示职工信息
ifstream ifs;
ifs.open("information.txt", ios::in);
if (!ifs.is_open()) {
cout << "文件打开失败" << endl;
return;//文件打开失败,直接返回
}
char buf[1024] = { 0 };
if (!ifs.getline(buf, sizeof(buf))) {//当文件为空时
cout << "" << endl;//空一行
cout <<"无任何职工信息!" <<endl;
return;//函数结束,节约时间
}
ifs.close();
//第一种读
/*char buf[1024] = { 0 };
while (ifs >> buf)
{
cout << buf <<endl ;
}*/
//第二种读
ifs.open("information.txt", ios::in);
while (ifs.getline(buf, sizeof(buf)))
{
cout << buf << endl;
}
//第三种读
/*string buf;
while (getline(ifs, buf))
{
cout << buf << endl;
}*/
//第四种读
/*char c;
while ((c = ifs.get()) != EOF) {
cout << c << endl;
}*/
ifs.close();
}
void delete_inf() {//删除离职员工信息,判断是否存在该编号。先开一个临时拷贝文件,在写入这个文件时跳过要删除的行,然后
//修改文件名为原文件名,以此实现删除特定行功能
//int id[1024] = { 0 };//存储职工编号
int id = 0;
int i = 0;
char buf[1024] = { 0 };
cout << "待删除职工编号为:";
cin >> id;
ifstream ifs;//读入文件流
ofstream ofs;//写入文件流
ofs.open("temp.txt",ios::out);
ifs.open("information.txt", ios::in);
if (!ifs.is_open()) {
cout << "打开文件失败!" << endl;
return;
}
int position = locate(id);//待删除行所在文件哪一行
//cout << "待删信息位于文件" << position << "行" << endl;
if (position == -1) {
cout << "该职工编号不存在!" << endl;
cout << "" << endl;
}
else {
while (!ifs.eof()) {
ifs.getline(buf, sizeof(buf));//读一行到buf里,getline第三个参数为分隔符,默认为回车
//ifs.getline()
if (i != position) {
ofs << buf << "\n";//把buf里的写进文件
}
i++;
}
ifs.close();
ofs.close();
if (remove("information.txt") == 0) {
cout << "删除成功!" << endl;
}
else {
cout << "删除失败" << endl;
}
if (rename("temp.txt", "information.txt") == 0) {
cout << "重命名成功!" << endl;
}
else {
cout << "重命名失败" << endl;
}
}
}
int locate(int id) {//根据编号返回所在行
int flag = 0;
ifstream ifs;
ifs.open("information.txt", ios::in);
char buf[1024] = { 0 };
while (ifs.getline(buf, sizeof(buf))) {
/*cout << "buf[10] = " << buf[10] << endl;
cout << "id = " << id << endl;*/
if (buf[10] - '0' == id) {//char型数字与int型数字差48个ASCII码值
break;
}
else
{
flag++;
//cout << "flag = " << flag << endl;
}
}
if (ifs.peek() == EOF) {//判断是否读到最后
return -1;
}
else
{
return flag;
}
ifs.close();
}
void clear_inf() {//清空职工所有信息,清空前要确认
ofstream ofs;
ofs.open("information.txt", ios::out);//ios::out如果文件已存在,打开时清除原有内容
ofs.close();
}
void update_inf() {
int id = 0;
int i = 0;
char buf[1024] = { 0 };
ifstream ifs;
ofstream ofs;
ofs.open("temp.txt", ios::out);
ifs.open("information.txt", ios::in);
cout << "输入要修改职工的编号:" << endl;
cin >> id;
int position = locate(id);//该id位于文件的某一行
if (position == -1) {
cout << "未找到该职工信息!" << endl;
}
else {
cout << "待修改职工信息为:" << endl;
while (!ifs.eof()) {
ifs.getline(buf, sizeof(buf));//读一行到buf里,getline第三个参数为分隔符,默认为回车
//ifs.getline()
if (i == position) {
insert_inf();
}
else {
ofs << buf<<"\n";
}
i++;
}
}
ifs.close();
ofs.close();
if (remove("information.txt") == 0) {
cout << "删除成功!" << endl;
}
else {
cout << "删除失败" << endl;
}
if (rename("temp.txt", "information.txt") == 0) {
cout << "重命名成功!" << endl;
}
else {
cout << "重命名失败" << endl;
}
}
void search_inf() {
int id = 0;
int i = 0;
char buf[1024]={ 0 };
cout << "输入要查找职工的编号:" << endl;
cin >> id;
ifstream ifs;
ifs.open("information.txt", ios::in);
int position = locate(id);//该id位于文件的某一行
if (position == -1) {
cout << "未找到该职工信息!" << endl;
}
else {
while (!ifs.eof()) {
ifs.getline(buf, sizeof(buf));//读一行到buf里,getline第三个参数为分隔符,默认为回车
//ifs.getline()
if (i == position) {
cout << "查找职工信息如下:" << endl;
cout << buf << endl;
cout << "" << endl;
}
i++;
}
}
ifs.close();
}
int main() {
int i = 1;
int j;//批量增加职工的数量
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;
while (i != 0)
{
cout << "请输入您要使用的功能:" << endl;
cin >> i;
switch (i)
{
case 1://增加职工信息,可以批量添加
cout << "添加信息的职工数量:" << endl;
cin >> j;
for (int i = 0; i < j; i++)
{
cout << "输入第" << i + 1 << "位职工信息" << endl;
insert_inf();
cout << "" << endl;
}
cout << "" << endl;
break;
case 2://展示职工信息,编号、姓名、岗位、职责
display_inf();
cout << "" << endl;
break;
case 3://删除职工信息,根据职工编号删除
delete_inf();
break;
case 4://按照职工编号查找并修改
update_inf();
break;
case 5://按照编号或姓名,一共两种查找方式
search_inf();
break;
case 6:
case 7:
int i;
cout << "是否确认清空所有信息?1:确认 0:取消" << endl;
cin >> i;
if (i == 0) {
break;
}
else if(i == 1){
clear_inf();
cout << "清除完毕!" << endl;
cout << "" << endl;
break;
}
else {
cout << "输入错误,请输入正确的数字" << endl;
break;
}
default:
cout << "请输入正确的功能编号!" << endl;
cout << "" << endl;
break;
}
}
cout << "欢迎下次使用!" << endl;
return 0;
}
三、问题总结
1、文件删除和文件重命名
都在cstdio.h里定义
删除:
函数int remove(const char* filename);
返回值:删除成功返回0,发生错误则非0。
如果要删除的文件已被进程打开,则:
POSIX 系统- 如果名称是文件的最后一个链接,但任何进程仍然打开该文件,则该文件将一直存在,直到最后一个正在运行的进程关闭该文件。
Windows - 如果文件仍被任何进程打开,则不允许删除该文件。
remove()也可以用于删除目录。
重命名:
函数:int rename( const char *oldname, const char *newname );
返回值:重命名成功返回0,发生错误则非0。
重命名文件时,如果newname指定文件已存在,则该源文件会被删除。//这点有待验证
rename()还可以用于移动到文件到不同的位置,通过给文件的newname提供不同的路径来完成。
并且rename()也可以为目录重命名。
2、文件删除和重命名失败问题:
在rename和remove之前要把读写流关掉。
int delete_file(const char* filename) {
if (remove(filename) == 0) {
return 1;
}
else {
return -1;
}
}
int main() {
ofstream ofs;
ofs.open("example.txt", ios::out | ios::app);
ofs << "实例";
ofs.close();//删之前先关掉
if (delete_file("example.txt") == 1) {
cout << "删除成功" << endl;
}
else {
cout << "删除失败" << endl;
}
return 0;
}
2、char型数字和int型数字向转换问题
char型数字比int型数字ASCII码多48,因此
char转int:
而’0’的ASCII码为48
char a = '1';
int b = 1;
a - '0' == b;//为真
int转char:
char a = '1';
int b = 1;
b + '0' == a;//为真
3、char数组存汉字的问题
一个汉字占两个字节,因此每个汉字需要两个连续char数组元素来存放,其中每个数组元素会把汉字截断,因此没法通过输出某个数组元素来得到相应汉字。
4、删除文本文件指定行
C++没法直接删除文本文件中的指定行,但是可以逐行读取,写入另一个临时文件,当都的第N行的时候,跳过这一行的写入。
写完后,删除源文件,重命名临时文件即可。
文章展示了用C++编程实现一个简单的职工管理系统,包括添加、删除、修改和查找职工信息功能,所有数据存储于本地文本文件。系统处理不同类型的职工,如普通员工、经理和老板,且提供了文件操作的细节,如如何按编号删除文件中的特定行。
2418

被折叠的 条评论
为什么被折叠?



