我想把程序的一些配置文件保存下来,用.ini文件应该比较简单,但我不知道操作.ini文件的函数,谁能告诉我,给个例子,加点注解!谢谢

解决方案 »

  1.   

    WritePrivateProfile...
    GetPrivateProfile...WriteProfile...
    GetProfile...在MSDN的INDEX写下上边的任意一个东西,会看到好多相似的函数,就用他们。
      

  2.   

    ::GetPrivateProfileString()具体查msdn
      

  3.   

    CString strIPAddress("192.168.0.1");
    CString strFileName("c:\\test.ini");::WritePrivateProfileString("NetSetup", "IPAddress", strIPAddress,  strFileName);你的ini文件内容如下[NetSetup]
    IPAddress=192.168.0.1读取如下CString strAcceptIP
    ::GetPrivateProfileString("NetSetup", "IPAddress","DefaultValue",strAcceptIP.GetBuffer(15), 16,strFileName);strAcceptIP.ReleaseBuffer();
      

  4.   

    GetCurrentDirectory(255,inipath);
    strcat(inipath,"\\path.ini");
    GetPrivateProfileString("Model",A,"NULL",model.GetBuffer(MAX_PATH),MAX_PATH,inipath);ini file [Model]
    A=xxx结果
      model==xxx
      

  5.   

    GetPrivateProfileInt();
    GetPrivateProfileString();
    WritePrivateProfileSection();
    WritePrivateProfileString();
      

  6.   

    一个操作ini文件的类:
    .h
    // IniFile.h: interface for the CIniReader class.
    //
    //////////////////////////////////////////////////////////////////////#if !defined(AFX_INIFILE_H__99976B4B_DBA1_4D1E_AA14_CBEB63042FD1__INCLUDED_)
    #define AFX_INIFILE_H__99976B4B_DBA1_4D1E_AA14_CBEB63042FD1__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000#include <afxcoll.h>class CIniReader  
    {
    public:
    // method to set INI file name, if not already specified 
    void setINIFileName(CString strINIFile);

    // methods to return the lists of section data and section names
    CStringList* getSectionData(CString strSection);
    CStringList* getSectionNames();

    // check if the section exists in the file
    BOOL sectionExists(CString strSection); // updates the key value, if key already exists, else creates a key-value pair
    long setKey(CString strValue, CString strKey, CString strSection); // give the key value for the specified key of a section
    CString getKeyValue(CString strKey,CString strSection);

    // default constructor
    CIniReader()
    {
    m_sectionList = new CStringList();
    m_sectionDataList = new CStringList();
    }

    CIniReader(CString strFile)
    {
    m_strFileName = strFile;
    m_sectionList = new CStringList();
    m_sectionDataList = new CStringList();
    }

    ~CIniReader()
    {
    delete m_sectionList;
    delete m_sectionDataList;
    }

    private:
    // lists to keep sections and section data
    CStringList *m_sectionDataList;
    CStringList *m_sectionList;

    CString m_strSection;
    long m_lRetValue;

    // ini file name 
    CString m_strFileName;
    };#endif // !defined(AFX_INIFILE_H__99976B4B_DBA1_4D1E_AA14_CBEB63042FD1__INCLUDED_).cpp
    // IniFile.cpp: implementation of the CIniReader class.
    //
    //////////////////////////////////////////////////////////////////////#include "stdafx.h"
    //#include "Readini.h"
    #include "INI.h"
    #include <afxcoll.h>#ifdef _DEBUG
    #undef THIS_FILE
    static char THIS_FILE[]=__FILE__;
    #define new DEBUG_NEW
    #endif//////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////// Used to retrieve a value give the section and key
    CString CIniReader::getKeyValue(CString strKey,CString strSection)
    {
    char ac_Result[255]; // Get the info from the .ini file
    m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,
    "",ac_Result, 255, (LPCTSTR)m_strFileName); CString strResult(ac_Result);
    return strResult;
    }// Used to add or set a key value pair to a section
    long CIniReader::setKey(CString strValue, CString strKey, CString strSection)
    {
    m_lRetValue = WritePrivateProfileString (strSection, strKey, 
                                 strValue, m_strFileName); return m_lRetValue;
    }// Used to find out if a given section exists
    BOOL CIniReader::sectionExists(CString strSection)
    {
    char ac_Result[100];
    CString csAux;
    // Get the info from the .ini file
    m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL,
    "",ac_Result, 90, (LPCTSTR)m_strFileName);
    // Return if we could retrieve any info from that section
    return (m_lRetValue > 0);
    }// Used to retrieve all of the  section names in the ini file
    CStringList* CIniReader::getSectionNames()  //returns collection of section names
    {
    char ac_Result[2000];
    m_sectionList->RemoveAll();

    m_lRetValue = GetPrivateProfileSectionNames(ac_Result,2000,(LPCTSTR)m_strFileName);

    CString strSectionName;
    for(int i=0; i<m_lRetValue; i++)
    {
    if(ac_Result[i] != '\0') {
    strSectionName = strSectionName + ac_Result[i];
    } else {
    if(strSectionName != "") {
    m_sectionList->InsertAfter(m_sectionList->GetTailPosition(),strSectionName);
    }
    strSectionName = "";
    }
    } return m_sectionList;
    }// Used to retrieve all key/value pairs of a given section.  
    CStringList* CIniReader::getSectionData(CString strSection)  
    {
    char ac_Result[2000];  //change size depending on needs
    m_sectionDataList->RemoveAll();
    m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, 2000, (LPCTSTR)m_strFileName); CString strSectionData;
    for(int i=0; i<m_lRetValue; i++)
    {
    if(ac_Result[i] != '\0') {
    strSectionData = strSectionData + ac_Result[i];
    } else {
    if(strSectionData != "") {
    m_sectionDataList->InsertAfter(m_sectionDataList->GetTailPosition(),strSectionData);
    }
    strSectionData = "";
    }
    } return m_sectionDataList;
    }void CIniReader::setINIFileName(CString strINIFile)
    {
    m_strFileName = strINIFile;
    }
      

  7.   

    GetPrivateProfileInt,不光是INI文件,我试了其他文件,只要按格式都行
      

  8.   

    应用程序初始化中把配置文件的路径付给CWinApp的m_pslProFile成员变量中,然后调用CWinApp中与读写配置文件相关的函数就可以了
      

  9.   

    自己写一个也没关系的
    用CFILE/CSTDIOFILE很容易实现
      

  10.   

    概述在程序中经常要用到设置或者其他少量数据的存盘,以便程序在下一次执行的时候可以使用,比如说保存本次程序执行时窗口的位置、大小、一些用户设置的数据等等,在 Dos 下编程的时候,我们一般自己产生一个文件,由自己把这些数据写到文件中,然后在下一次执行的时候再读出来使用。在 Win32 编程中当然你也可以这样干,但 Windows 已经为我们提供了两种方便的办法,那就是使用注册表或者 ini 文件(Profile)来保存少量数据。本文中先介绍一下 .ini 文件的使用。ini 文件是文本文件,中间的数据格式一般为:
    [Section1 Name]
    KeyName1=value1
    KeyName2=value2
    ...[Section2 Name]
    KeyName1=value1
    KeyName2=value2ini 文件可以分为几个 Section,每个 Section 的名称用 [] 括起来,在一个 Section 中,可以有很多的 Key,每一个 Key 可以有一个值并占用一行,格式是 Key=value,Win32 对 ini 文件操作的 api 中,有一部分是对 win.ini 操作的,有一部分是对用户自定义的 ini 文件操作的。Win.in 和 system.ini 是Windows的两个非常重要的初始化文件,Windows将用户所作的选择以及各种变化的系统信息记录在这两个文件中。System.ini 描述了系统硬件的当前状态,Win.ini 文件则包含了Windows 系统运行环境的当前配置。由于 Win.ini 文件的重要性和常用性,Win32 中有专门对 Win.ini 进行操作的 api,它们是:GetProfileInt - 从 Win.ini 文件的某个 Section 取得一个 key 的整数值,它的原形是:GetProfileInt( 
    LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
    LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址
    INT nDefault // 如果 Key 值没有找到,则返回缺省的值是多少
    ); 如果 Key 值没有找到的话,返回值是 nDefault 指定的缺省值,如果 Key 中的值是负数,则返回 0,如果 Key 指定的是数字和字符串的混合,则返回数字部分的值,比如说 x=1234abcd,则返回 1234
    GetProfileString - 从 Win.ini 文件的某个 Section 取得一个 key 的字符串,它的原形是:GetProfileString(
    LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
    LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址
    LPCTSTR lpDefault, // 如果 Key 值没有找到,则返回缺省的字符串的地址
    LPTSTR lpReturnedString, // 返回字符串的缓冲区地址
    DWORD nSize // 缓冲区的长度
    ); 返回的字符串在缓冲区内,返回的 eax 值是返回的字符串的长度(不包括尾部的0)
    GetProfileSection - 从 Win.ini 文件中读出整个 Section 的内容,它的原形是:GetProfileSection(
    LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
    LPTSTR lpReturnedString, // 返回数据的缓冲区地址
    DWORD nSize // 返回数据的缓冲区长度
    ); 
    WriteProfileSection - 将一个整个 Section 的值 写入 Win.ini 文件的指定 Section 中,它的原形是:WriteProfileSection(
    LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
    LPCTSTR lpString // 要写入的数据的地址
    );如果 Win.ini 没有指定的 Section,API 会新建立一个并写入数据,如果已经存在,则先删除原来 Seciton 中所有的 Key 值然后写入新的。
    WriteProfileString - 将一个 Key 值写入 Win.ini 文件的指定 Section 中,它的原形是:WriteProfileString( 
    LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
    LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址
    LPCTSTR lpString // 要写的字符串地址
    );如果 Win.ini 没有指定的 Section,API 会新建 Section,如果没有指定的 Key 则新建一个 Key 并写入数据,如果已经存在,则用字符串代替原来的值。 
    以上的 Api 是对 Win.ini 操作的,当然对于我们来说,用的更多的是在程序运行的目录中建立自己的 ini 文件,如果需要对自己的 ini 文件操作,就要用到另一组 Api,这一组 api 和上面的很象,只要把上面一组的 Profile 换成 PrivateProfile(私有的)就可以了,参数中也相应的多了一个 ini 文件名的参数。例如 GetPrivateProfileInt、GetPrivateProfileSection、WritePrivateProfileString 等等, 下面分别介绍:GetPrivateProfileInt - 从 ini 文件的某个 Section 取得一个 key 的整数值,它的原形是:GetPrivateProfileInt( 
    LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
    LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址
    INT nDefault // 如果 Key 值没有找到,则返回缺省的值是多少
    LPCTSTR lpFileName // ini 文件的文件名
    ); 中间参数和返回值的定义和 GetProfileInt 是一样的。
    GetPrivateProfileString - 从 ini 文件的某个 Section 取得一个 key 的字符串,它的原形是:GetPrivateProfileString(
    LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
    LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址
    LPCTSTR lpDefault, // 如果 Key 值没有找到,则返回缺省的字符串的地址
    LPTSTR lpReturnedString, // 返回字符串的缓冲区地址
    DWORD nSize // 缓冲区的长度
    LPCTSTR lpFileName // ini 文件的文件名 
    ); 
    GetPrivateProfileSection - 从 ini 文件中读出整个 Section 的内容,它的原形是:GetPrivateProfileSection(
    LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
    LPTSTR lpReturnedString, // 返回数据的缓冲区地址
    DWORD nSize // 返回数据的缓冲区长度
    LPCTSTR lpFileName // ini 文件的文件名 
    ); 这个 api 可以读出整个 section 的内容,当你不知道 section 中有哪些 key 的时候,可以使用这个 api 将整个 section 读出后再处理。
    GetPrivateProfileSectionNames - 从 ini 文件中获得 Section 的名称,它的原形是:GetPrivateProfileSectionNames( 
    LPTSTR lpszReturnBuffer, // 返回数据的缓冲区地址
    DWORD nSize // 返回数据的缓冲区长度
    LPCTSTR lpFileName // ini 文件的文件名 
    );如果 ini 中有两个 Section: [sec1] 和 [sec2],则返回的是 'sec1',0,'sec2',0,0 ,当你不知道 ini 中有哪些 section 的时候可以用这个 api 来获取名称
    WritePrivateProfileSection - 将一个整个 Section 的内容入 ini 文件的指定 Section 中,它的原形是:WritePrivateProfileSection(
    LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
    LPCTSTR lpString // 要写入的数据的地址
    LPCTSTR lpFileName // ini 文件的文件名
    );
    WritePrivateProfileString - 将一个 Key 值写入 ini 文件的指定 Section 中,它的原形是:WritePrivateProfileString( 
    LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址
    LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址
    LPCTSTR lpString // 要写的字符串地址
    LPCTSTR lpFileName // ini 文件的文件名
    );如果 ini 中没有指定的 Section,API 会新建 Section,如果没有指定的 Key 则新建一个 Key 并写入数据,如果已经存在,则用字符串代替原来的值。当指定的 ini 也不存在的时候,API 会自动建立一个新的文件,所以使用 ini 的好处是我们不必为了保存少量的数据涉及到文件操作,就连查找文件是否存在的操作都不必要。使用要点:在我们实际使用的时候,用的最多的是 GetPrivateProfileString 和 WritePrivateProfileString,但在对自定义 ini 文件操作的时候要注意的是,如果 lpFileName 指定的文件没有路径的话,Api 会去 Windows 的安装目录去找而不会在当前目录找,但是每次用到 ini 函数要获取当前路径显然太麻烦了,这里有一个变通的办法,你只要在 ini 文件名前面加上 .\ 就可以了,比如说要对本目录下的 user.ini 操作,那么文件名就是 '.\user.ini' 这样显然比较方便。另外,当你要把一个 Key 清除的时候,可以使用把 lpString 指向一个空的字符串然后使用 WritePrivateProfileString。当你要把一个 section 的全部内容清空的时候,也不必把 key 一个个的清除,可以使用把 lpString 指向一个空的字符串然后使用 WritePrivateProfileSection。
      

  11.   

    支持《 回复人: guoxiny(狼) ( ) 信誉:100  2003-06-06 09:07:00  得分:0