如何程序一运行,就读取INI文件。我很急,恳请帮助

解决方案 »

  1.   

    在应用程序的InitInstance函数里
      

  2.   

    InitialDialog()
    InitInstance()里面用ar关联一个CString来读 怎么处理就看你自己了
      

  3.   

    MFC向导生成的程序框架中有一个InitInstance()或对话框程序的InitialDialog()消息函数,这两个函数在程序一运时就会被执行,因此加到这两个函数里的代码也会在程序一运行的初始就开始运行。你所要做的,就是在这个两个函数中加入你读取和处理INI文件的代码即可实现你的目的。读取INI有API函数,如下:
    GetPrivateProfileInt 为初始化文件(.ini文件)中指定的条目获取一个整数值 
    GetPrivateProfileSection 获取指定小节(在.ini文件中)所有项名和值的一个列表 
    GetPrivateProfileString 为初始化文件中指定的条目取得字串 
      

  4.   

    GetPrivateProfileString
    The GetPrivateProfileString function retrieves a string from the specified section in an initialization file. This function is provided for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry. DWORD GetPrivateProfileString(
      LPCTSTR lpAppName,        // points to section name
      LPCTSTR lpKeyName,        // points to key name
      LPCTSTR lpDefault,        // points to default string
      LPTSTR lpReturnedString,  // points to destination buffer
      DWORD nSize,              // size of destination buffer
      LPCTSTR lpFileName        // points to initialization filename
    );
     
    Parameters
    lpAppName 
    Pointer to a null-terminated string that specifies the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer. 
    lpKeyName 
    Pointer to the null-terminated string containing the key name whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter. 
    lpDefault 
    Pointer to a null-terminated default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. This parameter cannot be NULL. 
    Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks. Windows 95: Although lpDefault is declared as a constant parameter, the system strips any trailing blanks by inserting a null character into the lpDefault string before copying it to the lpReturnedString buffer. Windows NT: The system does not modify the lpDefault string. This means that if the default string contains trailing blanks, the lpReturnedString and lpDefault strings will not match when compared using the lstrcmp function. lpReturnedString 
    Pointer to the buffer that receives the retrieved string. 
    nSize 
    Specifies the size, in characters, of the buffer pointed to by the lpReturnedString parameter. 
    lpFileName 
    Pointer to a null-terminated string that names the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory. 
    Return Values
    The return value is the number of characters copied to the buffer, not including the terminating null character. If neither lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string is truncated and followed by a null character, and the return value is equal to nSize minus one. If either lpAppName or lpKeyName is NULL and the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters. In this case, the return value is equal to nSize minus two. Res
      

  5.   

    在你的App类的InitInstance函数里面,
    使用 GetPrivateProfileString
      

  6.   


    #include "StdAfx.h" 
    #include "ini.h" #define MAX_ALLSECTIONS 2048 //全部的段名 
    #define MAX_SECTION 260 //一个段名长度 
    #define MAX_ALLKEYS 6000 //全部的键名 
    #define MAX_KEY 260 //一个键名长度 BOOL CIni::SetFileName(LPCTSTR lpFileName) 

    CFile file; 
    CFileStatus status; if(!file.GetStatus(lpFileName,status)) 
    return TRUE; m_strFileName=lpFileName; 
    return FALSE; 
    } CString CIni::GetFileName(void) 

    return m_strFileName; 
    } BOOL CIni::SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate) 

    TCHAR lpTemp[MAX_PATH] ={0}; //以下if语句表示如果设置bCreate为false时,当没有这个键名时则返回TRUE(表示出错) 
    //!*&*none-value*&!* 这是个垃圾字符没有特别意义,这样乱写是防止凑巧相同。 
    if (!bCreate) 

    GetPrivateProfileString(lpSection,lpKey,"!*&*none-value*&!*",lpTemp,MAX_PATH,m_strFileName); 
    if(strcmp(lpTemp,"!*&*none-value*&!*")==0) 
    return TRUE; 
    } if(WritePrivateProfileString(lpSection,lpKey,lpValue,m_strFileName)) 
    return FALSE; 
    else 
    return GetLastError(); 
    } CString CIni::GetValue(LPCTSTR lpSection, LPCTSTR lpKey) 

    DWORD dValue; 
    TCHAR lpValue[MAX_PATH] ={0}; dValue=GetPrivateProfileString(lpSection,lpKey,"",lpValue,MAX_PATH,m_strFileName); 
    return lpValue; 
    } BOOL CIni::DelSection(LPCTSTR lpSection) 

    if(WritePrivateProfileString(lpSection,NULL,NULL,m_strFileName)) 
    return FALSE; 
    else 
    return GetLastError(); 
    } BOOL CIni::DelKey(LPCTSTR lpSection, LPCTSTR lpKey) 

    if(WritePrivateProfileString(lpSection,lpKey,NULL,m_strFileName)) 
    return FALSE; 
    else 
    return GetLastError(); 

    int CIni::GetSections(CStringArray& arrSection) 

    /* 
    本函数基础: 
    GetPrivateProfileSectionNames - 从 ini 文件中获得 Section 的名称 
    如果 ini 中有两个 Section: [sec1] 和 [sec2],则返回的是 'sec1',0,'sec2',0,0 ,当你不知道 
    ini 中有哪些 section 的时候可以用这个 api 来获取名称 
    */ 
    int i; 
    int iPos=0; 
    int iMaxCount; 
    TCHAR chSectionNames[MAX_ALLSECTIONS]={0}; //总的提出来的字符串 
    TCHAR chSection[MAX_SECTION]={0}; //存放一个段名。 
    GetPrivateProfileSectionNames(chSectionNames,MAX_ALLSECTIONS,m_strFileName); //以下循环,截断到两个连续的0 
    for(i=0;i<MAX_ALLSECTIONS;i++) 

    if (chSectionNames[i]==0) 
    if (chSectionNames[i]==chSectionNames[i+1]) 
    break; 
    } iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。 
    arrSection.RemoveAll();//清空原数组 for(i=0;i<iMaxCount;i++) 

    chSection[iPos++]=chSectionNames[i]; 
    if(chSectionNames[i]==0) 

    arrSection.Add(chSection); 
    memset(chSection,0,MAX_SECTION); 
    iPos=0; 
    } } return (int)arrSection.GetSize(); 
    } int CIni::GetKeyValues(CStringArray& arrKey,CStringArray& arrValue, LPCTSTR lpSection) 

    /* 
    本函数基础: 
    GetPrivateProfileSection- 从 ini 文件中获得一个Section的全部键名及值名 
    如果ini中有一个段,其下有 "段1=值1" "段2=值2",则返回的是 '段1=值1',0,'段2=值2',0,0 ,当你不知道 
    获得一个段中的所有键及值可以用这个。 
    */ 
    int i; 
    int iPos=0; 
    CString strKeyValue; 
    int iMaxCount; 
    TCHAR chKeyNames[MAX_ALLKEYS]={0}; //总的提出来的字符串 
    TCHAR chKey[MAX_KEY]={0}; //提出来的一个键名 GetPrivateProfileSection(lpSection,chKeyNames,MAX_ALLKEYS,m_strFileName); for(i=0;i<MAX_ALLKEYS;i++) 

    if (chKeyNames[i]==0) 
    if (chKeyNames[i]==chKeyNames[i+1]) 
    break; 
    } iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。 
    arrKey.RemoveAll();//清空原数组 
    arrValue.RemoveAll(); for(i=0;i<iMaxCount;i++) 

    chKey[iPos++]=chKeyNames[i]; 
    if(chKeyNames[i]==0) 

    strKeyValue=chKey; 
    arrKey.Add(strKeyValue.Left(strKeyValue.Find("="))); 
    arrValue.Add(strKeyValue.Mid(strKeyValue.Find("=")+1)); 
    memset(chKey,0,MAX_KEY); 
    iPos=0; 
    } } return (int)arrKey.GetSize(); 
    } BOOL CIni::DelAllSections() 

    int nSection; 
    CStringArray arrSection; 
    nSection=GetSections(arrSection); 
    for(int i=0;i<nSection;i++) 

    if(DelSection(arrSection[i])) 
    return GetLastError(); 

    return FALSE; 
    }