zoukankan      html  css  js  c++  java
  • WinMoblie 利用EVC读写INI,XML配置文件

    WINCE

    平台利用evc读写inixml格式配置文件 

     

     

         为了解决代码的可以执行和可重用性,我们一般会利用一些配置来达到我们的目的。目前流行的配置文件有用INI文件的,也有很多利用XML文件的,或者写数据库,写注册表的。像.Net平台可以直接添加应用程序的配置文件(app.config),同时它还提供了相关的类库来操作这种特定类型的配置文件,在Win32环境下也提供了诸如GetPrivateProfileInt(),GetPrivateProfileString()等API来实现读取ini文件。但是在PPC环境中,不管是读取XML,还是ini文件,都没有提供相关的库来供我们调用,这样就需要我们自己实现一个类似的方法来读取我们所需的XML或者ini类似的配置文件了,不过幸运的是,很多慷慨的同志们已经将他们的代码公布于网上了,如果要理解利用EVC或者VC操作XML文件,请查看

    http://hi.baidu.com/yxifu/blog/item/fa1569225bda52a44623e8f0.html

     

    本文主要讲解利用EVC读写ini文件

    u       ini 文件格式

    ini 文件是文本文件,中间的数据格式一般为:

    [Section1 Name]

    KeyName1=value1

    KeyName2=value2

    ...

     

    [Section2 Name]

    KeyName1=value1

    KeyName2=value2

     

    文件可以分为几个 Section,每个 Section 的名称用 [] 括起来,在一个 Section 中,可以有很多的 Key,每一个 Key 可以有一个值并占用一行,格式是 Key=value

    u       读写原理
    将文件的整个内容看成是一个字符串,读写配置节和配置内容就相当于对字符串做操作,利用GetBuffer, Find, Mid()方法都配置文件,利用Delete,Insert()方法写文件。

    注:本文提供的代码也是从网上搜索而来,在自己调试后,发现读写都有点小问题,修改了下,同时这里也约定,每个Keyvalue之间不能有空格,不然程序会出错。

    其中注释的地方为我修改过的地方

    文件示例
    [GPS]

    Interval=5000

     

    代码示例:

     1.写内容

    u       Demo下载
    http://download.csdn.net/source/773349


    BOOL CProfile::WriteProfileString(
    const CString strSection, const CString strEntry, const CString strValue, const CString strIniPath)
    {
    if(strSection == L"" || strEntry == L"" || strValue == L"" || strIniPath == L"")
    {
    return FALSE;
    }
    CFile IniFile;
    CString strCombine;

    TRY
    {
    if(! IniFile.Open(strIniPath, CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate))
    {
    return FALSE;
    }

    if(IniFile.GetLength() == 0)
    {
    strCombine
    = L"[" + strSection + L"]" + L"\r\n"
    + strEntry + L"=" + strValue + L"\r\n";
    LPTSTR lpCombine
    = strCombine.GetBuffer(0);
    IniFile.Write(lpCombine, strCombine.GetLength()
    * 2);
    IniFile.Close();
    return TRUE;
    }

    WCHAR
    *pBuf;
    pBuf
    = new WCHAR[IniFile.GetLength() / 2 + 1];
    if(pBuf == NULL)
    {
    IniFile.Close();
    return FALSE;
    }
    if(IniFile.Read(pBuf, IniFile.GetLength()) != IniFile.GetLength())
    {
    delete[] pBuf;
    IniFile.Close();
    return FALSE;
    }

    pBuf[IniFile.GetLength()
    / 2] = NULL;
    strCombine.GetBuffer(MAX_LEN);
    strCombine
    = pBuf;
    delete[] pBuf;

    int iIndex1, iIndex2, iIndex3, iIndexT;
    iIndex1
    = strCombine.Find(L"[" + strSection + L"]\r\n");
    if(iIndex1 == -1)
    {
    strCombine
    += L"[" + strSection + L"]" + L"\r\n"
    + strEntry + L"=" + strValue + L"\r\n";

    LPTSTR lpCombine
    = strCombine.GetBuffer(0);
    IniFile.SetLength(
    0);
    IniFile.SeekToBegin();
    IniFile.Write(lpCombine, strCombine.GetLength()
    * 2);
    IniFile.Close();
    return TRUE;
    }
    //iIndexT = iIndex1 + 4 + strSection.GetLength();

    //modify by plr at 2008-11-8
    iIndexT = iIndex1 + strSection.GetLength() + 2; //2代表"[]"两个字符

    iIndex2
    = strCombine.Find(strEntry + L"=", iIndexT);
    if(iIndex2 == -1)
    {
    strCombine.Insert(iIndexT, strEntry
    + L"=" + strValue + L"\r\n");

    LPTSTR lpCombine
    = strCombine.GetBuffer(0);
    IniFile.SetLength(
    0);
    IniFile.SeekToBegin();
    IniFile.Write(lpCombine, strCombine.GetLength()
    * 2);
    IniFile.Close();
    return TRUE;
    }
    else
    {
    iIndex3
    = strCombine.Find(L"\r\n", iIndex2 + 1);
    if(iIndex3 == -1)
    {
    IniFile.Close();
    return FALSE;
    }
    iIndexT
    = iIndex2 + 1 + strEntry.GetLength();
    strCombine.Delete(iIndexT, iIndex3
    - iIndexT);
    strCombine.Insert(iIndexT, strValue);

    LPTSTR lpCombine
    = strCombine.GetBuffer(0);
    IniFile.SetLength(
    0);
    IniFile.SeekToBegin();
    IniFile.Write(lpCombine, strCombine.GetLength()
    * 2);
    IniFile.Close();
    return TRUE;
    }

    }
    CATCH(CFileException, e)
    {
    }
    END_CATCH

    IniFile.Close();
    return FALSE;
    }
    
    
    写整型
    
    

    2.读配置文件

    Code
    
    


     

    获取整型
    
    

     

    
    
    作者彭立云
    本文版权归作者所有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    【转】130 个相见恨晚的超实用网站,一次性分享出来
    基于maven testng extentreport git jenkins 持续自动化测试
    TestNG与ExtentReport集成
    Jenkins集成jSonarQube
    SonarQube & Sonar Scanner
    Jenkins集成jacoco收集单元测试覆盖率
    Jenkins集成jacoco收集集成测试覆盖率
    Jacoco收集单元测试、集成测试和系统功能测试覆盖率
    SoanrQube7.4安装(Window)
    SoanrQube使用maven进行代码分析
  • 原文地址:https://www.cnblogs.com/hanchan/p/1333081.html
Copyright © 2011-2022 走看看