当前位置:编程学习 > XML/UML >>

VC使用tinyxml读写INI,cfg,XML配置文件

#define UPDATE_CONFIG "update.cfg" //客户端版本信息文件 
    //解析Update.cfg文件 
    TiXmlDocument UpdateCfgDoc(UPDATE_CONFIG); 
    if (!UpdateCfgDoc.LoadFile()) 
    { 
        ::MessageBox(NULL, "读取客户端版本文件失败!", NULL, MB_ICONERROR); 
        SwitchStateAsync(US_UpdateFailed); 
        return 0; 
    } 
    TiXmlElement* pConfigRoot = UpdateCfgDoc.RootElement()->FirstChildElement(); 
    if (NULL == pConfigRoot) 
    { 
        return 0; 
    } 
    const char* szCurVer = pConfigRoot->Attribute("ClientVersion"); 
    const char* szCurToolVer = pConfigRoot->Attribute("UpdateToolVersion"); 
 
 
//保存玩家当前选择服务器到配置文件 
#define SERVER_CONFIG "LoginServer.cfg" //保存玩家选择的区 
    TiXmlDocument ServerConfigDoc(SERVER_CONFIG); 
    if (!ServerConfigDoc.LoadFile()) 
    { 
        ::MessageBox(NULL, "解析本地服务器选择配置文件失败!", NULL, MB_ICONERROR); 
        return; 
    } 
    CString strLoginServerName; 
    m_CurLoginServer.GetWindowText(strLoginServerName); 
    TiXmlElement* pHistoryServer = ServerConfigDoc.RootElement()->FirstChildElement(); 
    if(pHistoryServer!=NULL) 
    { 
        if(strcmp(pHistoryServer->Value(), "server") == 0) 
            pHistoryServer->SetAttribute("name", strLoginServerName); 
            ServerConfigDoc.SaveFile(); 
    } 
    else 
    { 
        TiXmlElement ele("server"); 
        ele.SetAttribute("name",strLoginServerName); 
        ServerConfigDoc.RootElement()->LinkEndChild(&ele); 
        ServerConfigDoc.SaveFile(); 
    } 


摘自 破空的专栏
补充:软件开发 , Vc ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,