一个有string 和vector的类如何保存到文件 并且能读取
#ifndef MYQQ_H
#define MYQQ_H
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
class myQQ
{
public:
myQQ();
myQQ(string Name,string Location,int Year,int Month,int Day,int Age);
myQQ(myQQ& qq);
friend ostream& operator<<(ostream& out,myQQ QQ)
{
out<<"QQ号码"<<QQ.QQnumber<<endl;
out<<"姓名:"<<QQ.name<<endl;
out<<"Q龄:"<<QQ.age<<endl;
out<<"地址:"<<QQ.location<<endl;
out<<"出生年月:"<<QQ.year<<"年"<<QQ.month<<"月"<<QQ.day<<"日"<<endl;
return out;
}
friend ostream& operator<<(ostream& out,myQQ *QQ)
{
out<<"QQ号码"<<QQ->QQnumber<<endl;
out<<"姓名:"<<QQ->name<<endl;
out<<"Q龄:"<<QQ->age<<endl;
out<<"地址:"<<QQ->location<<endl;
out<<"出生年月:"<<QQ->year<<"年"<<QQ->month<<"月"<<QQ->day<<"日"<<endl;
return out;
}
void add();
vector<myQQ> getmyQQList(){return myQQlist;}
FILE *in;
FILE *out;
void save();
void read(myQQ);
static int number;
static int count;
protected:
vector<myQQ> myQQlist;
string name;
string location;
int QQnumber;
int year;
int month;
int day;
int age;
int password;
bool is_WB;
bool is_WX;
bool is_Coded;
};
#endif // MYQQ_H
[解决办法]
共你参考。
//考虑到你用了中文,需要使用在windowsstd::locale("chs")
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <locale>
#include <algorithm>
#include <iterator>
using namespace std;
class myQQ
{
public:
friend wostream& operator<<(wostream& out,myQQ QQ)
{
out<<L"QQ号码: "<< QQ.QQnumber <<endl;
out<<L"姓名: "<< (QQ.name.empty() ? L"未知" : QQ.name)<< endl;
out<<L"Q龄: "<< QQ.age << endl;
out<<L"地址: "<< (QQ.location.empty() ? L"未知" : QQ.location) << endl;
out<<L"出生年月: "<< QQ.year<<L"/"<<QQ.month<<L"/"<<QQ.day<<endl;
return out;
}
friend wistream& operator>>(wistream& is,myQQ &QQ)
{
std::wstring key;
wchar_t sep;
is >> key >> QQ.QQnumber >> key >> QQ.name >> key >> QQ.age >> key >> QQ.location >> key >> QQ.year >> sep >> QQ.month >> sep>> QQ.day;
return is;
}
protected:
wstring name;
wstring location;
int QQnumber;
int year;
int month;
int day;
int age;
int password;
bool is_WB;
bool is_WX;
bool is_Coded;
};
int main()
{
{
vector<myQQ> myQQlist;
myQQlist.push_back(myQQ());
myQQlist.push_back(myQQ());
std::wofstream ofs("some.dat");
ofs.imbue(std::locale("chs"));
std::copy(myQQlist.begin(), myQQlist.end(), std::ostream_iterator<myQQ,wchar_t>(ofs, L"\n"));
}
{
vector<myQQ> myQQlist;
std::wifstream ifs("some.dat");
ifs.imbue(std::locale("chs"));
std::wcout.imbue(std::locale("chs"));
std::copy(std::istream_iterator<myQQ,wchar_t>(ifs), std::istream_iterator<myQQ, wchar_t>(), std::back_inserter(myQQlist));
std::copy(myQQlist.begin(), myQQlist.end(),
std::ostream_iterator<myQQ,wchar_t>(std::wcout, L"\n---------------\n"));
}
}
std::ifstream ifs(file_name.c_str());
if (!ifs.is_open())
{
return(false);
}
const int bufsiz = 4096;
char buffer[bufsiz];
while (!ifs.eof())
{
ifs.getline(buffer, bufsiz);
std::string message(buffer);
base_trim(message, ' '); // 去除头尾的空格
base_trim(message, '\r'); // 去除(头)尾的'\r' (windows文件在linux下就会有'\r')
base_trim(message, '\n'); // 这个应该没必要
// 然后你从message中解析出你要的
}