栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 练习day8

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 练习day8

文件输入输出

#include
using namespace std;
#include
#include
#include
#include
class Person {
private:
	char name[20];
	char id[18];
	int age;
	char addr[20];
public:
	Person() {};
	Person(char *n, char* pid, int Age, char* Addr) {
		strcpy_s(name, n);//name[]退化为指针
		strcpy_s(id, pid);
		age = Age;
		strcpy_s(addr, Addr);

	}
	void display() {
		cout << name << "t" << id << "t" << age << "t" << addr << endl;
	}
};
int main(int argc, const char* argv[])
{
	vectorv;
	char ch;
	ofstream out("d:/person.dat", ios::out | ios::app | ios::binary);
	char Name[20], ID[18], Addr[20];
	int Age;
	cout << "------输入个人档案------" << endl << endl;
	do {
		cout << "姓名: ";
		cin >> Name;
		cout << "身份证号: ";
		cin >> ID;
		cout << "年龄: ";
		cin >> Age;
		cout << "地址: ";
		cin >> Addr;
		Person per(Name, ID, Age, Addr);
		out.write((char*)&per, sizeof(per));
		cout << "enter another person" << endl;
		cin >> ch;
	} while (ch == 'y');
	out.close();
	ifstream in("d:/person.dat", ios::in | ios::binary);
	Person s;
	in.read((char*)&s, sizeof(s));
	//eof()函数可以帮助我们用来判断文件是否为空,抑或是判断其是否读到文件结尾。
	while (!in.eof())
	{
		v.push_back(s);
		in.read((char*)&s, sizeof(s));
	}
	cout << "n---------从文件中读出的数据--------" << endl << endl;//L15
	auto pos = v.begin();
	for (pos = v.begin(); pos != v.end(); pos++)
		(*pos).display();

	system("pause");
	return 0;
}
#include 
#include 
#include 
using namespace std;
class Employee{
private:
    int number ,age;
    char name[20];
    double sal;
public:
    Employee(){}
    Employee(int num,char* Name,int Age, double Salary){
        number=num;
        strcpy(name,Name);
        age=Age;
        sal=Salary;
    }
    void display(){
        cout< 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/347838.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号