• 全部
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

如何遍历INI文件[section]下所有的值

nineclock 2004-11-29 03:15:50
如何遍历INI文件[section]下所有的值??
比如它有
[Registry]
Root: HKCU; Subkey: my; ValueType: string; ValueName: ...; ValueData: {app}\my.exe;
Root: HKCU; Subkey: my; ValueType: string; ValueName: MyID; ValueData: 12345;
如果得知他有两个子项???
...全文
448 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
mixtrue 2004-11-29
我封装的ini 类【类似于Delphi使用习惯】
// IniFilesDef.h: interface for the CIniFile class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_INIFILES_IMP_H__810C0A02_A7DF_4D48_A32E_033ADAF59235__INCLUDED_)
#define AFX_INIFILES_IMP_H__810C0A02_A7DF_4D48_A32E_033ADAF59235__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

typedef int INT ;

typedef struct
{
} INIITEMS;


class CIniFile
{
public:
CIniFile();
CIniFile (LPCTSTR );
//viratul function , can be rewritten

virtual ~CIniFile();
virtual CString ReadStrings (const char* Section,const char* Ident,const char* Default);
virtual BOOL WriteString (const char* Section,const char* Ident,const char* Value);

virtual INT ReadInteger (const char* Section,const char* Ident,int Default);
virtual BOOL WriteInteger(const char* Section,const char* Ident,int Value);

virtual BOOL ReadBoolean (const char* Section,const char* Ident,bool Default);
virtual BOOL WriteBoolean(const char* Section,const char* Ident,bool Value);

virtual BOOL ReadSection (const char* Section,CStringList & cstLst);

virtual BOOL SectionExists(const char* Section);
virtual void UpdateFile ();

virtual BOOL EraseSection (const char* Section );
virtual BOOL DeleteKey (const char* Section ,const char* Ident);
void SetIniFileName (const char* );
protected :

LPSTR m_IniFileName;

};

#endif // !defined(AFX_INIFILES_IMP_H__810C0A02_A7DF_4D48_A32E_033ADAF59235__INCLUDED_)

+++++++++++++++++++++++++++

.cpp 文件
+++++++++++++++++++++++++++
// IniFileDef.cpp: implementation of the CIniFile class.
// Purpose: To implement myself INI file class.
// Author: Mixtrue mixtrue@hotmail.com
// Date: MAY 8 2004
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "IniFile.h"
#include "IniFilesDef.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define MAXSIZE 2048
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


CIniFile::CIniFile() : m_IniFileName("./Configuration.ini")
{

}
CIniFile::CIniFile(LPCTSTR FileName) : m_IniFileName("./Configuration.ini")
{
m_IniFileName =(char*) FileName;
}

CIniFile::~CIniFile()
{
}
void CIniFile::SetIniFileName (const char* FileName)
{
m_IniFileName=(char*) FileName;
}
//Read a string from a configuration file
CString CIniFile::ReadStrings (const char* Section,const char* Ident,const char* Default)
{
char ReturnedString[100]="";
CString strStr="";
GetPrivateProfileString(Section,Ident,"",ReturnedString,sizeof(ReturnedString),m_IniFileName);
strStr=ReturnedString;
if (strStr.GetLength==0)
strStr=Default;
else
strStr =ReturnedString;

return strStr;
}
//Write a string to configuration file
BOOL CIniFile::WriteString (const char* Section,const char* Ident,const char* Value)
{
return WritePrivateProfileString(Section,Ident,Value,m_IniFileName);
}
//To write an integer to configuration
BOOL CIniFile::WriteInteger(const char* Section,const char* Ident,int Value)
{
char buff [9]="";
itoa(Value,buff,10);
return WriteString(Section,Ident,buff);
}
//Read an Integer value from specific section and Ident.
INT CIniFile::ReadInteger (const char* Section,const char* Ident,int Default=0)
{
CString strOut;
strOut= ReadStrings (Section,Ident,"0");
if (strOut.GetLength ()==0)
strOut="0";
return atoi(strOut.GetBuffer (strOut.GetLength ()));
}
//Read a boolean value from specific section and Ident.
BOOL CIniFile::ReadBoolean (const char* Section,const char* Ident,bool Default)
{
CString strIn,strOut;
if (Default)
strIn="TRUE";
else
strIn="FALSE";
strIn.MakeUpper ();

strOut =ReadStrings (Section,Ident,strIn.GetBuffer (strIn.GetLength ()));
strOut.MakeUpper ();
if ((strOut == "FALSE") || (strOut.GetLength ()==0))
return false;
else
return true;
}
//Write a bool value to file
BOOL CIniFile::WriteBoolean (const char* Section,const char* Ident,bool Value)
{
if (Value)
return WriteString (Section,Ident,"TRUE");
else
return WriteString (Section,Ident,"FALSE");

}
//Erase a section
BOOL CIniFile::EraseSection (const char* Section )
{
return WritePrivateProfileString(Section,NULL,NULL,m_IniFileName);
}
//Delete a key
BOOL CIniFile:eleteKey (const char* Section ,const char* Ident)
{
return WritePrivateProfileString(Section,Ident,NULL,m_IniFileName);
}
//Reflush the File .
void CIniFile::UpdateFile ()
{
WritePrivateProfileString(NULL, NULL, NULL, m_IniFileName);
}

BOOL CIniFile::SectionExists(const char* Section)
{
//LVITEM lvIm;
return true;
}


BOOL CIniFile::ReadSection (const char* Section,CStringList & cstLst)
{
char buff[MAXSIZE];
char* p=NULL;
CString strTemp;
cstLst.RemoveAll ();

int ReadSize=GetPrivateProfileString(Section, NULL, NULL, buff, MAXSIZE,m_IniFileName);
if (!ReadSize) {
TRACE0(_T("Can not read specific Section from Configuration file"));
return false;
} else {
strTemp=buff; p=buff;
while ((*p)!='\0') {
p+= strTemp.GetLength()+1;
cstLst.AddHead (p);
}
return true;
}
}
回复
ChengZi 2004-11-29
使用下边这个函数读出所有项, 然后根据NULL的个数计算各个子项.
DWORD GetPrivateProfileSection(
LPCTSTR lpAppName,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);

回复
nineclock 2004-11-29
如何读所有项?
回复
yzhouen 2004-11-29
读出section下的所有项,然后你根据子项的特点来判断它有几个子项
回复
yzhouen 2004-11-29
那就把section下的所有项,然后你根据子项的特点来判断它有几个子项
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2004-11-29 03:15
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……