过几天写给你看吧
授人以渔,不授人以鱼。
/* 题目来源:<span style="color: #008000; text-decoration: underline;">https://bbs.bccn.net/thread-415375-1-1.html[/color] 编程环境:Visual Studio 2012 C++ 2013.06.25 */ #include <cstdio> #include <cstdlib> #include <vector> #include <conio.h> #include "Employee.h" void Pause(void); bool Init(void); const char* file_name = "data.txt"; void main(void) { // 构建原始数据 Init(); printf_s("文件中的原始数据\n"); std::vector<Employee*> employees = Employee::Load_Data(file_name); Employee::List(employees); Pause(); // 删除一条记录并储存到文件中 employees[1]->Delete(); Employee::Save_Data(file_name, employees, 0, employees.size()); printf_s("\n\n删除后的数据\n"); Employee::List(Employee::Load_Data(file_name), false); Pause(); } void Pause(void) { printf_s("\nPress any key to continue..."); _getch(); } // 生成原始数据文件 bool Init(void) { Employee* employees[] = { new Employee(110, 1, "张三", 25, 1900), new Employee(111, 2, "李四", 28, 1700), new Employee(112, 3, "王五", 30, 1900) }; FILE* file; errno_t error_code = fopen_s(&file, file_name, "wb"); if (error_code != 0) { printf_s("File %s open error: %d\n", file_name, error_code); return false; } for (size_t index = 0; index < _countof(employees); ++index) { Employee::Write_Record(*employees[index], file); } fclose(file); return true; }
#ifndef _EMPLOYEE #define _EMPLOYEE class Employee { private: bool _deleted; // 删除标志 int _id; // 工号 int _type; // 职工类型 char _name[20]; // 姓名 int _age; // 年龄 int _pay; // 工资 public: Employee(); Employee(int id, int type, const char* name, int age, int pay); void Display(void); const char* const ToString(void); void Delete(void); void Recall(void); bool IsDeleted(void); static Employee* Read_Record(FILE* file); static void Write_Record(Employee& record, FILE* file); static std::vector<Employee*> Load_Data(const char* file_name); static bool Save_Data(const char* file_name, std::vector<Employee*> employees, int start, size_t count); static void List(std::vector<Employee*>& employees, bool show_deleted = true); }; #endif
#include <cstdio> #include <cstring> #include <vector> #include "Employee.h" static const char* Employee_Type[] = { "教师", "临时教师", "主任" }; Employee::Employee() { _deleted = false; _id = 0; _type = 0; _name[0] = '\0'; _age = 0; _pay = 0; } Employee::Employee(int id, int type, const char* name, int age, int pay) { _deleted = false; _id = id; _type = type; strncpy_s(_name, name, sizeof(_name) - 1); _age = age; _pay = pay; } void Employee::Display(void) { printf_s("工号: %03d\n", _id); printf_s("类型: %s\n", Employee_Type[_type - 1]); printf_s("姓名: %s\n", _name); printf_s("年龄: %d\n", _age); printf_s("工资: %d\n\n", _pay); } const char* const Employee::ToString(void) { const size_t buffer_size = 100; char* buffer = new char[buffer_size]; if (buffer != nullptr) { sprintf_s(buffer, buffer_size, "%c%03d %-10s %-20s %2d %6d", _deleted ? '*' : ' ', _id, Employee_Type[_type - 1], _name, _age, _pay); } return buffer; } void Employee::Delete(void) { _deleted = true; } void Employee::Recall(void) { _deleted = false; } bool Employee::IsDeleted(void) { return _deleted; } Employee* Employee::Read_Record(FILE* file) { Employee* e = new Employee; if ((e == nullptr) || fread(e, sizeof(Employee), 1, file) != 1) { e = nullptr; } return e; } void Employee::Write_Record(Employee& record, FILE* file) { fwrite(&record, sizeof(Employee), 1, file); } std::vector<Employee*> Employee::Load_Data(const char* file_name) { std::vector<Employee*> employees; FILE* file; errno_t error_code = fopen_s(&file, file_name, "rb"); if (error_code == 0) { Employee* e; while (e = Employee::Read_Record(file)) { employees.push_back(e); } fclose(file); } else { printf_s("File %s open error: %d\n", file_name, error_code); } return employees; } bool Employee::Save_Data(const char* file_name, std::vector<Employee*> employees, int start, size_t count) { bool result(true); FILE* file; errno_t error_code = fopen_s(&file, file_name, "wb"); if (error_code != 0) { printf_s("File %s create error: %d\n", file_name, error_code); result = false; } for (size_t index = start; index < start + count; ++index) { Employee::Write_Record(*employees[index], file); } fclose(file); return result; } void Employee::List(std::vector<Employee*>& employees, bool show_deleted) { for (std::vector<Employee*>::const_iterator iterator = employees.cbegin(); iterator != employees.cend(); ++iterator) { if (!(*iterator)->IsDeleted() || show_deleted) { const char* const buffer = (*iterator)->ToString(); printf_s("%s\n", buffer); delete buffer; } } }