jq 输出二进制文件_作业(5)实现二进制文件输入输出(难度3/10)

本作业主要考察:C++二进制文件读写

二进制文件读写是格式化数据的持久化的基础,就算是像数据库这种大型的复杂的系统软件,其原理也就是建立在格式化文件读写的基础上的。

C++中级程序员教程

C++读写文本文件的请移步这里

需求如下:

c5599f8edf1322cfd008e877a4f74a25.png

实现代码完成下面的测试用例,要求和预期输出结果一致

#include <iostream>
#include <string>
#include <fstream>
#include <list>
using namespace std;

struct Record
{
	static constexpr int FIRST_NAME_LEN = 10;
	static constexpr int SECOND_NAME_LEN = 10;
	static constexpr int SOCIAL_SECURITY_LEN = 9;
	static constexpr int DEP_ID_LEN = 9;

public:
	Record(void);
	friend istream& operator>>(istream& in, Record& record);
	friend ostream& operator<<(ostream& out, const Record& record);
	ofstream& WriteToBinaryFile(ofstream& out) const;
	ifstream& Record::ReadFromBinaryFile(ifstream& fin);
	void WriteToOstream(ostream& os) const;
public:
	char m_firstName[FIRST_NAME_LEN];
	char m_secondName[SECOND_NAME_LEN];
	char m_socialSecurity[SOCIAL_SECURITY_LEN];
	char m_departmentID[DEP_ID_LEN];
	int m_years;
	int m_salary;
	int m_averageSalary;
	int m_averageYears;
};
bool ReadTextEmployee(const string& textFileName, list<Record>& listEmployee);
void PrintAllEmployee(const list<Record>& listEmployee);
bool WriteEmployeeToBinaryFile(const list<Record>& listEmployee, const string& textFileName);
bool ReadBinaryEmployee(const string& fileName, list<Record>& listEmployee);
bool WriteEmployeeToTextFile(const list<Record>& listEmployee, const string& textFileName);
void CalculateAverage(list<Record>& listEmployee);
void PrintAllEmployeeWithAverage(const list<Record>& listEmployee);

int main(int arcg, char** argv)
{
	list<Record> listEmployee;

	cout<<"ReadTextEmployee from employee.in"<<endl;
	if (!ReadTextEmployee("employee.in", listEmployee))
	{
		return -1;
	}
	PrintAllEmployee(listEmployee);

	cout<<"WriteEmployeeToBinaryFile to employee.binaryn"<<endl;
	WriteEmployeeToBinaryFile(listEmployee, "employee.binary");

	listEmployee.clear();

	cout<<"ReadBinaryEmployee from employee.binary"<<endl;
	ReadBinaryEmployee("employee.binary", listEmployee);
	PrintAllEmployee(listEmployee);

	CalculateAverage(listEmployee);
	PrintAllEmployeeWithAverage(listEmployee);
	WriteEmployeeToTextFile(listEmployee, "employee.out");

	return 0;
}

预期输出结果:

338232babc911304cb24cc86d74b4c4a.png

employee.ini

Bill Tarpon     182460678  789   8   30600
Fred Caldron    456905434  789   10  40700
Sally Bender    203932239  790   8   50000
David Kemp      568903493  790    9  60000

402bb025345e053e1fae10ddc05e7f5d.png

来吧!

C++中级程序员教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值