1.序列化读写
/***单个类的写入***/
CStudentDTO dtoA(1,_T("nameA"),11);
CStudentDTO dtoB(2,_T("nameB"),22);
CFile file;
file.Open(_T("c:\\data.data"),CFile::modeCreate|CFile::modeWrite);
CArchive ar(&file,CArchive::store);
ar.WriteObject(&dtoA);
ar.Flush();
file.Close();
file.Open(_T("c:\\data.data"),CFile::modeRead);
CArchive br(&file,CArchive::load);
CObList *pDtoList = (CObList *)br.ReadObject(CObject::GetThisClass());
file.Close();
/***list<类>的写入***/
CStudentDTO dtoA(1,_T("nameA"),11);
CStudentDTO dtoB(2,_T("nameB"),22);
CFile file;
CObList list;
list.AddTail(&dtoA);//这里写入的对象dtoA对应的类CStudentDTO必须是派生于CObject的类
list.AddTail(&dtoB);
file.Open(_T("c:\\data.data"),CFile::modeCreate|CFile::modeWrite);
CArchive ar(&file,CArchive::store);
ar.WriteObject(&list);
ar.Flush();
file.Close();
file.Open(_T("c:\\data.data"),CFile::modeRead);
CArchive br(&file,CArchive::load);
CObList *pDtoList = (CObList *)br.ReadObject(CObList::GetThisClass());
file.Close();
POSITION pos = pDtoList->GetHeadPosition();
while(pos!=NULL)
{
CStudentDTO * stute = (CStudentDTO*)pDtoList->GetAt(pos);
wstring d = stute->GetName();
string tt(d.begin(),d.end());
cout<<tt<<endl;
pDtoList->GetNext(pos);
}
2. ifstream/ofstream
void CFileDAO::Save(CMyList<CStudentDTO> &list)
{
ofstream out;
out.open("sort.txt");
//out.open("D:\\ok.data",ios::binary);
CStudentDTO *p;
list.GetHead(p);
for(int i = 0;i<list.fact_len;i++)
out<<p[i].GetID()<<"\t"<<p[i].GetName()<<"\t"<<p[i].GetChineseScore()<<"\t"
<<p[i].GetEnglishScore()<<'\n';
out.close();
}
void CFileDAO::Read(CMyList<CStudentDTO> &list)
{
ifstream in;
in.open("sort.txt");
//in.open("D:\\ok.data",ios::binary);
int id;
char name[20];
float chineseScore, englishScore;
if(in)
{
while(!in.eof())
{
in>>id>>name>>chineseScore>>englishScore;
CStudentDTO dto(id,name,chineseScore,englishScore);
mylist.push_back(dto);
}
}
in.close();
}
或者
void CFileDAO::Save(CMyList<CStudentDTO> &list)
{
ofstream file;
file.open("D:\\ok.data",ios::binary);
file<<mylist.size();
list<CStudentDTO>::iterator q;
for(q = mylist.begin();q!=mylist.end();p++)
{
CStudentDTO dto = *q;
file.write((char *)&dto,sizeof(CStudentDTO));
}
file.close();
}
void CFileDAO::Read(CMyList<CStudentDTO> &list)
{
ifstream file;
file.open("D:\\ok.data",ios::binary);
list<CStudentDTO>::iterator p;
int fact_num;
file>>fact_num;
for(int i = 0;i<fact_num;i++)
{
CStudentDTO dto;
file.read((char *)&dto,sizeof(CStudentDTO));
mylist.push_back(dto);
}
file.close();
}
3.数据库sqlite3中数据的读写
void MyDataBaseDAO::Save(list<CStudentDTO> &mylist)
{
try{
CppSQLite3DB db;
cout << "SQLite Version: " << db.SQLiteVersion() << endl;
TCHAR path[255] = {0};
GetModuleFileName(NULL,path,255);
(_tcsrchr(path,'\\'))[1]='\0';
string sd = path;
sd = sd + _T("test.db");
string ssss(sd.begin(),sd.end());
cout<<"SQLiteVersion:"<<db.SQLiteVersion()<<endl;
char *pppp = (char*)sd.c_str();
db.open(ssss.c_str());
//db.open("C:\\Users\\drilistbox\\Desktop\\test.db");
string cmd;
int nRows;
stringstream strstream;
strstream << "delete from stutable;";
cmd = strstream.str();
list<CStudentDTO>::iterator p;
for(p = mylist.begin();p!=mylist.end();p++)
{
strstream << "insert into stutable values (" << p->GetID() << ",'"<< p->GetName() <<"',"<< p->GetChineseScore() <<","<<p->GetEnglishScore()<< ");";
cmd = strstream.str();
}
nRows = db.execDML(cmd.c_str());//nRows:表示影响的行数
cout << nRows << " rows updated" << endl;
db.close();
}
catch (CppSQLite3Exception& e)
{
cerr << e.errorCode() << ":" << e.errorMessage() << endl;
}
}
void MyDataBaseDAO::Read(list<CStudentDTO> &mylist)
{
try{
CppSQLite3DB db;
TCHAR path[255] = {0};
GetModuleFileName(NULL,path,255);
(_tcsrchr(path,'\\'))[1]='\0';
string sd = path;
sd = sd + _T("test.db");
string ssss(sd.begin(),sd.end());
//cout<<"SQLiteVersion:"<<db.SQLiteVersion()<<endl;
char *pppp = (char*)sd.c_str();
db.open(ssss.c_str());
//db.open("C:\\Users\\drilistbox\\Desktop\\test.db");
CppSQLite3Buffer bufSQL;
bufSQL.format("select * from stutable;");
CppSQLite3Query query = db.execQuery(bufSQL);
while (!query.eof())
{
mylist.push_back(CStudentDTO(query.getIntField(0),query.getStringField(1),query.getFloatField(2),query.getFloatField(3)));
query.nextRow();
}
query.finalize(); //清除query的储存空间,否则内存泄漏
db.close();
}
catch (CppSQLite3Exception& e)
{
cerr << e.errorCode() << ":" << e.errorMessage() << endl;
}
}