33,311
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
struct person {
char firstName[128];
char lastName[128];
char number[8];
person(const char s1[], const char s2[], const char s3[])
{
strncpy(firstName, s1, sizeof(firstName));
strncpy(lastName, s2, sizeof(lastName));
strncpy(number, s3, sizeof(number));
}
};
void write(ofstream& out, person& per);
void read (ifstream& in, person& per);
int main()
{
person a("aa", "bb", "1");
ofstream out("persons.txt",ios::out | ios::binary);
write(out,a);
out.close();
person b("d", "d", "2");
ifstream in("persons.txt",ios::in | ios::binary);
read(in,b);
cout <<b.firstName <<b.lastName;
return 0;
}
void write(ofstream& out, person& per)
{
out.write(reinterpret_cast <char*>(&per),sizeof(per));
}
void read (ifstream& in, person& per)
{
in.read(reinterpret_cast <char*>(&per), sizeof(per));
}