文件读取

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值