==很急,真的很急==怎么用手直接写配置文件,配置文件有什么格式,是干什么用的,怎么用C去操作??求求各位帮帮我了这个老菜了!

lxigb 2002-02-26 10:39:36
多谢
...全文
125 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
strip 2002-02-27
  • 打赏
  • 举报
回复
void SetSize(int nNewSize, int nGrowBy)
{
// ASSERT_VALID(this);
ASSERT(nNewSize >= 0);

if (nGrowBy != -1)
m_nGrowBy = nGrowBy; // set new size

if (nNewSize == 0)
{
// shrink to nothing
if (m_pData != NULL)
{
DestructElements<TYPE>(m_pData, m_nSize);
delete[] (BYTE*)m_pData;
m_pData = NULL;
}
m_nSize = m_nMaxSize = 0;
}
else if (m_pData == NULL)
{
// create one with exact size
#ifdef SIZE_T_MAX
ASSERT(nNewSize <= SIZE_T_MAX/sizeof(TYPE)); // no overflow
#endif
m_pData = (TYPE*) new BYTE[nNewSize * sizeof(TYPE)];
ConstructElements<TYPE>(m_pData, nNewSize);
m_nSize = m_nMaxSize = nNewSize;
}
else if (nNewSize <= m_nMaxSize)
{
// it fits
if (nNewSize > m_nSize)
{
// initialize the new elements
ConstructElements<TYPE>(&m_pData[m_nSize], nNewSize-m_nSize);
}
else if (m_nSize > nNewSize)
{
// destroy the old elements
DestructElements<TYPE>(&m_pData[nNewSize], m_nSize-nNewSize);
}
m_nSize = nNewSize;
}
else
{
// otherwise, grow array
int nGrowBy = m_nGrowBy;
if (nGrowBy == 0)
{
// heuristically determine growth when nGrowBy == 0
// (this avoids heap fragmentation in many situations)
nGrowBy = m_nSize / 8;
nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy);
}
int nNewMax;
if (nNewSize < m_nMaxSize + nGrowBy)
nNewMax = m_nMaxSize + nGrowBy; // granularity
else
nNewMax = nNewSize; // no slush

ASSERT(nNewMax >= m_nMaxSize); // no wrap around
#ifdef SIZE_T_MAX
ASSERT(nNewMax <= SIZE_T_MAX/sizeof(TYPE)); // no overflow
#endif
TYPE* pNewData = (TYPE*) new BYTE[nNewMax * sizeof(TYPE)];

// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(TYPE));

// construct remaining elements
ASSERT(nNewSize > m_nSize);
ConstructElements<TYPE>(&pNewData[m_nSize], nNewSize-m_nSize);

// get rid of old stuff (note: no destructors called)
delete[] (BYTE*)m_pData;
m_pData = pNewData;
m_nSize = nNewSize;
m_nMaxSize = nNewMax;
}
}


int Append(const CArrayPP& src)
{
ASSERT_VALID(this);
ASSERT(this != &src); // cannot append to itself

int nOldSize = m_nSize;
SetSize(m_nSize + src.m_nSize);
CopyElements<TYPE>(m_pData + nOldSize, src.m_pData, src.m_nSize);
return nOldSize;
}


void Copy(const CArrayPP& src)
{
ASSERT_VALID(this);
ASSERT(this != &src); // cannot append to itself

SetSize(src.m_nSize);
CopyElements<TYPE>(m_pData, src.m_pData, src.m_nSize);
}


void FreeExtra()
{
ASSERT_VALID(this);

if (m_nSize != m_nMaxSize)
{
// shrink to desired size
#ifdef SIZE_T_MAX
ASSERT(m_nSize <= SIZE_T_MAX/sizeof(TYPE)); // no overflow
#endif
TYPE* pNewData = NULL;
if (m_nSize != 0)
{
pNewData = (TYPE*) new BYTE[m_nSize * sizeof(TYPE)];
// copy new data from old
memcpy(pNewData, m_pData, m_nSize * sizeof(TYPE));
}

// get rid of old stuff (note: no destructors called)
delete[] (BYTE*)m_pData;
m_pData = pNewData;
m_nMaxSize = m_nSize;
}
}


void SetAtGrow(int nIndex, ARG_TYPE newElement)
{
// ASSERT_VALID(this);
ASSERT(nIndex >= 0);

if (nIndex >= m_nSize)
SetSize(nIndex+1, -1);
m_pData[nIndex] = newElement;
}


void InsertAt(int nIndex, ARG_TYPE newElement, int nCount /*=1*/)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0); // will expand to meet need
ASSERT(nCount > 0); // zero or negative size not allowed

if (nIndex >= m_nSize)
{
// adding after the end of the array
SetSize(nIndex + nCount, -1); // grow so nIndex is valid
}
else
{
// inserting in the middle of the array
int nOldSize = m_nSize;
SetSize(m_nSize + nCount, -1); // grow it to new size
// destroy intial data before copying over it
DestructElements<TYPE>(&m_pData[nOldSize], nCount);
// shift old data up to fill gap
memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],
(nOldSize-nIndex) * sizeof(TYPE));

// re-init slots we copied from
ConstructElements<TYPE>(&m_pData[nIndex], nCount);
}

// insert new value in the gap
ASSERT(nIndex + nCount <= m_nSize);
while (nCount--)
m_pData[nIndex++] = newElement;
}


void RemoveAt(int nIndex, int nCount)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0);
ASSERT(nCount >= 0);
ASSERT(nIndex + nCount <= m_nSize);

// just remove a range
int nMoveCount = m_nSize - (nIndex + nCount);
DestructElements<TYPE>(&m_pData[nIndex], nCount);
if (nMoveCount)
memmove(&m_pData[nIndex], &m_pData[nIndex + nCount],
nMoveCount * sizeof(TYPE));
m_nSize -= nCount;
}


void InsertAt(int nStartIndex, CArrayPP* pNewArray)
{
ASSERT_VALID(this);
ASSERT(pNewArray != NULL);
ASSERT_VALID(pNewArray);
ASSERT(nStartIndex >= 0);

if (pNewArray->GetSize() > 0)
{
InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
for (int i = 0; i < pNewArray->GetSize(); i++)
SetAt(nStartIndex + i, pNewArray->GetAt(i));
}
}


#ifdef _DEBUG


void AssertValid() const
{
CObject::AssertValid();

if (m_pData == NULL)
{
ASSERT(m_nSize == 0);
ASSERT(m_nMaxSize == 0);
}
else
{
ASSERT(m_nSize >= 0);
ASSERT(m_nMaxSize >= 0);
ASSERT(m_nSize <= m_nMaxSize);
ASSERT(AfxIsValidAddress(m_pData, m_nMaxSize * sizeof(TYPE)));
}
}
#endif //_DEBUG

};

#include "Str.h"

typedef CArrayPP <StrPP,StrPP> CStringArrayPP;

#undef new
#ifdef _REDEF_NEW
#define new DEBUG_NEW
#undef _REDEF_NEW
#endif

#ifdef _INC_NEW
#undef _INC_NEW
#endif

#endif
strip 2002-02-27
  • 打赏
  • 举报
回复
#ifndef ARRAYPP
#define ARRAYPP

#ifdef new
#undef new
#define _REDEF_NEW
#endif

#ifndef _INC_NEW
#include <new.h>
#endif

#ifndef ASSERT
#define ASSERT(f) ((void)0)
#define VERIFY(f) ((void)(f))
#define ASSERT_VALID(pOb) ((void)0)
#endif

#ifndef BYTE
typedef unsigned char BYTE;
#endif

#ifndef __AFXTEMPL_H__

template<class TYPE>
inline void __stdcall ConstructElements(TYPE* pElements, int nCount)
{
ASSERT(nCount == 0 ||
AfxIsValidAddress(pElements, nCount * sizeof(TYPE)));

// first do bit-wise zero initialization
memset((void*)pElements, 0, nCount * sizeof(TYPE));

// then call the constructor(s)
for (; nCount--; pElements++)
::new((void*)pElements) TYPE;
// ::new((void*)pElements) TYPE;
}


template<class TYPE>
inline void __stdcall DestructElements(TYPE* pElements, int nCount)
{
ASSERT(nCount == 0 ||
AfxIsValidAddress(pElements, nCount * sizeof(TYPE)));

// call the destructor(s)
for (; nCount--; pElements++)
pElements->~TYPE();
}
namespace {
template<class TYPE>
inline void __stdcall CopyElements(TYPE* pDest, const TYPE* pSrc, int nCount)
{
ASSERT(nCount == 0 ||
AfxIsValidAddress(pDest, nCount * sizeof(TYPE)));
ASSERT(nCount == 0 ||
AfxIsValidAddress(pSrc, nCount * sizeof(TYPE)));

// default is element-copy using assignment
while (nCount--)
*pDest++ = *pSrc++;
}
};
#endif // __AFXTEMPL_H__

/////////////////////////////////////////////////////////////////////////////
// CArrayPP<TYPE, ARG_TYPE>

template<class TYPE, class ARG_TYPE>
class CArrayPP
{

// Implementation
protected:
TYPE* m_pData; // the actual array of data
int m_nSize; // # of elements (upperBound - 1)
int m_nMaxSize; // max allocated
int m_nGrowBy; // grow amount

public:

// Attributes
__forceinline int GetSize() const
{ return m_nSize; }


__forceinline int GetUpperBound() const
{ return m_nSize-1; }

__forceinline void RemoveAll()
{ SetSize(0, -1); }

__forceinline TYPE GetAt(int nIndex) const
{ ASSERT(nIndex >= 0 && nIndex < m_nSize);
return m_pData[nIndex]; }

__forceinline void SetAt(int nIndex, ARG_TYPE newElement)
{ ASSERT(nIndex >= 0 && nIndex < m_nSize);
m_pData[nIndex] = newElement; }

__forceinline TYPE& ElementAt(int nIndex)
{ ASSERT(nIndex >= 0 && nIndex < m_nSize);
return m_pData[nIndex]; }

__forceinline const TYPE* GetData() const
{ return (const TYPE*)m_pData; }

__forceinline TYPE* GetData()
{ return (TYPE*)m_pData; }

__forceinline int Add(ARG_TYPE newElement)
{ int nIndex = m_nSize;
SetAtGrow(nIndex, newElement);
return nIndex; }

__forceinline TYPE operator[](int nIndex) const
{ return GetAt(nIndex); }

__forceinline TYPE& operator[](int nIndex)
{ return ElementAt(nIndex); }



CArrayPP()
{
m_pData = NULL;
m_nSize = m_nMaxSize = m_nGrowBy = 0;
}


~CArrayPP()
{
// ASSERT_VALID(this);

if (m_pData != NULL)
{
DestructElements<TYPE>(m_pData, m_nSize);
delete[] (BYTE*)m_pData;
}
}
lxigb 2002-02-26
  • 打赏
  • 举报
回复
?
gameboy999 2002-02-26
  • 打赏
  • 举报
回复
@_@
//**************************************************************************//
//
// What is an ini file and what's their 'name' , For Example:
//
// [Chat Server] <----------- this is called Section
//
// Server IP = 127.0.0.1 <------ We need to get this value
// ^\--- this is called Key
//
// ;Note: the item above is Server IP <----------- Notation
//
//****************************************************************************//


lxigb 2002-02-26
  • 打赏
  • 举报
回复
有没有基础一点的啊!!
斑竹啊,救救我吧!
lxigb 2002-02-26
  • 打赏
  • 举报
回复

非常感谢大家!
非常!
我现在连配置文件是什么都不知道啊!!
说起来惭愧!!
能不能请大家从最简单的开始给我讲一讲啊!
谢了!
lxigb 2002-02-26
  • 打赏
  • 举报
回复

非常感谢大家!
非常!
我现在连配置文件是什么都不知道啊!!
说起来惭愧!!
能不能请大家从最简单的开始给我讲一讲啊!
谢了!
gameboy999 2002-02-26
  • 打赏
  • 举报
回复
#include "EIniFile.h"

int EIniFile::OpenIniFile(char * filename)
{
if(!m_IniFile.Open(filename,"r+"))
return m_IniFile.Open(filename,"w+");
return m_IniFile.IsOpen();
}


char * EIniFile::GetValueByString(char * SectionName , char * KeyName , char * DefaultValue)
{
if(!m_IniFile.IsOpen()) return NULL;

m_IniFile.SeekToBegin();
long pos = 0;
char * string = NULL;
char * Section = NULL;
if( (Section = (char *)malloc( strlen(SectionName) + 3)) == NULL ) return NULL;

sprintf(Section,"[%s]",SectionName);

//首先定位Section!
while((!m_IniFile.IsEOF()) && (string = ReadOneLine(pos)))
{
pos = m_IniFile.Tell();

if(!strncmp(string,Section,strlen(Section))) //找到了该Section
{
free(string); //释放掉上个Section值
string = NULL;

while((!m_IniFile.IsEOF()) && (string = ReadOneLine(pos)))//开始寻找Key
{
pos = m_IniFile.Tell();

if(string[0] == '[') //不用查了,别浪费时间了,到了下一个Section了
{
free(string);
string = NULL;
free(Section);
return DefaultValue;
}

//";"号为注释符
if(string[0] == ';') { free(string); string = NULL; continue; }

if( !strncmp(string,KeyName,strlen(KeyName)) )
{ //找到了
strncpy(m_Value,string,MAX_VALUE_SIZE);
free(string);
string = NULL;
free(Section);
TrimValue();
return m_Value;
}

free(string);
string = NULL;
}
}// end if
free(string);
string = NULL;
}

if(Section) free(Section);
if(string) free(string);
//返回默认值
return DefaultValue;
}


//凡是调用了该函数的,一定调用free(string)
//来释放掉这个new出来的字符串
char * EIniFile::ReadOneLine(long pos)
{
//if(!m_IniFile.IsOpen()) return NULL;
m_IniFile.Seek(pos , SEEK_SET);
char value;
long count = 0;
while(!m_IniFile.IsEOF())
{
count += m_IniFile.ReadString(&value,1);
if (value == '\n') {value = '\0' ;break;}
}

if(count == 0) return NULL; //count == 0 说明连'\n'都没有了

char * string;
if( (string = (char *)malloc( count )) == NULL ) return NULL;
//为它增加一个中止符号,该中止符其实对语法分析并没有什么意义
//但是作为字符串显示的时候,则很有用处
//文件复位
m_IniFile.Seek(pos , SEEK_SET);
m_IniFile.ReadString( string, count - 1 );
string[count - 1] = '\0' ;
//向后读取一个空格
m_IniFile.ReadString(&value,1); //向后移动一格

int cnt = 0;
while(string[cnt] ==' ') cnt++;
strcpy(string,&string[cnt]);

return string;
}

void EIniFile::TrimValue()
{
for(int i = 0;i < MAX_VALUE_SIZE -1 ;i++)
if(m_Value[i] == '=') break;

if(i == MAX_VALUE_SIZE - 1)
{
m_Value[0] = '\0';
return;
}

strcpy(m_Value,&m_Value[i + 1]);

//消除两边的空格
//先去掉前面的空格
//下面这几句应该比 while(m_Value[0] =='') strcpy(m_Value,&m_Value[1]); 好
int count = 0;
while(m_Value[count] ==' ') count++;
strcpy(m_Value,&m_Value[count]);

//去掉后面的空格
count = strlen(m_Value) - 1;
while(m_Value[--count] == ' ') m_Value[count] = '\0';
}


int EIniFile::SetValueByString(const char * SectionName ,const char * KeyName ,const char * Value)
{
if(!m_IniFile.IsOpen()) return NULL;

m_IniFile.SeekToBegin();
long pos = 0;
char * string = NULL;
char * Section;
if( (Section = (char *)malloc( strlen(SectionName) + 3)) == NULL ) return NULL;

sprintf(Section,"[%s]",SectionName);

//首先定位Section!
while( ( !m_IniFile.IsEOF() ) && ( string = ReadOneLine(pos) ) )
{
pos = m_IniFile.Tell();

if(!strncmp(string,Section,strlen(Section))) //找到了该Section
{
free(string); //释放掉上个Section值

while((!m_IniFile.IsEOF()) && (string = ReadOneLine(pos)))//开始寻找Key
{

//";"号为注释符 这表明即使人家用KeyName ";keyname"来访问某个注释行,都是不行的
if(string[0] == ';')
{
pos = m_IniFile.Tell();
free(string);
string = NULL;
continue;
}

if(string[0] == '[') //不用查了,别浪费时间了,到了下一个Section了
{
//这里处理有Section但没有Key的情况
m_IniFile.Seek( pos ,SEEK_SET );
char value;
long count = 0;
while(!m_IniFile.IsEOF())
{
count += m_IniFile.ReadString(&value,1);
}
char * lastpart = (char *)malloc( count );

m_IniFile.Seek( pos , SEEK_SET );
m_IniFile.ReadString( lastpart, count );
m_IniFile.Seek( pos , SEEK_SET );

long cnt = m_IniFile.WriteString(KeyName,strlen(KeyName));
cnt += m_IniFile.WriteString("=" , 1);
cnt += m_IniFile.WriteString(Value,strlen(Value));
cnt += m_IniFile.WriteString("\n", 1 );
cnt += m_IniFile.WriteString(lastpart, count );

free(lastpart);
free(string);
free(Section);
if(cnt) return 0;

return -1;
}

if( !strncmp(string,KeyName,strlen(KeyName)) )
{ //找到了这个Key,将整行都替换掉
//前面处理
long pos2 = m_IniFile.Tell();

char value;
long count = 0;
m_IniFile.SeekToBegin();

while( (!m_IniFile.IsEOF()) && ( pos != m_IniFile.Tell()) )
{
count += m_IniFile.ReadString(&value,1);
}
char * firstpart = (char *)malloc( count );
m_IniFile.SeekToBegin();
m_IniFile.ReadString( firstpart, count );

m_IniFile.Seek( pos2 , SEEK_SET );
long count1 = 0;
while(!m_IniFile.IsEOF())
{
count1 += m_IniFile.ReadString(&value,1);
}

char * lastpart = (char *)malloc( count1 );
m_IniFile.Seek( pos2 , SEEK_SET );
m_IniFile.ReadString( lastpart, count1 );

ClearAll();

long cnt = m_IniFile.WriteString(firstpart, count);
cnt += m_IniFile.WriteString(KeyName,strlen(KeyName));
cnt += m_IniFile.WriteString("=" , 1);
cnt += m_IniFile.WriteString(Value,strlen(Value));
cnt += m_IniFile.WriteString("\n", 1 );
cnt += m_IniFile.WriteString(lastpart, count1 );

free(firstpart);
free(lastpart);
free(string);
free(Section);

if(cnt) return 0;
return -1;
}


pos = m_IniFile.Tell();
free(string);
string = NULL;
}

//------------------------------------------
//有Section,但是没找到Key,并且文件已经结束啦

m_IniFile.Seek( pos , SEEK_SET );

long cnt =m_IniFile.WriteString(KeyName,strlen(KeyName));
cnt += m_IniFile.WriteString("=" , 1);
cnt += m_IniFile.WriteString(Value,strlen(Value));
cnt += m_IniFile.WriteString("\n", 1 );

free(string);
free(Section);
if(cnt) return 0;
return -1;
//------------------------------------------------------------------

}// end if
free(string);
string = NULL;
}

if(Section) free(Section);
if(string) free(string);
//----------------------------------------------------
// 都没有找到,再最后添上[Section]和Key
//----------------------------------------------------
{
m_IniFile.SeekToEnd();
long cnt = m_IniFile.WriteString("[", 1 );
cnt += m_IniFile.WriteString(SectionName,strlen(SectionName));
cnt += m_IniFile.WriteString("]\n", 2 );
cnt += m_IniFile.WriteString(KeyName,strlen(KeyName));
cnt += m_IniFile.WriteString("=" , 1);
cnt += m_IniFile.WriteString(Value,strlen(Value));
cnt += m_IniFile.WriteString("\n", 1 );
if(cnt) return 2;
}
//返回失败值
return -1;

}

int EIniFile::ClearAll()
{
return (m_IniFile.Open(m_IniFile.GetFileName() , "w+"));
}


gameboy999 2002-02-26
  • 打赏
  • 举报
回复
EFile.h & EFile.cpp代码较简单,仅封装了Ansi 标准file操作
//**************************************************************************//
//
// What is an ini file and what's their 'name' , For Example:
//
// [Chat Server] <----------- this is called Section
//
// Server IP = 127.0.0.1 <------ We need to get this value
// ^\--- this is called Key
//
// ;Note: the item above is Server IP <----------- Notation
//
//****************************************************************************//


//--------------------------------------------//
// 封装对ini文件的基本操作 //
// //
// 使用符合ANSI标准的C RunTime函数 //
// 原则上可以不需改动直接移植到Unix //
// 等系统上 //
// Author: dah //2001/9/17 //
//--------------------------------------------//


#ifndef __DAH____EINIFILE_H__
#define __DAH____EINIFILE_H__

#include "EFile.h"

class EIniFile
{
public:
EIniFile(){};
EIniFile(char * filename){ OpenIniFile(filename); }
~EIniFile(){ m_IniFile.Close(); }

//Open the file for reading and writing
//if no such file exist, create a new one
int OpenIniFile(char * filename);
int CloseIniFile(){ return m_IniFile.Close(); }
int ClearAll(); //清空文件

//返回的值就是获得的值,其实就放在了m_Value里面
char * GetValueByString(char * SectionName , char * KeyName , char * DefaultValue);

//--------------------
//返回值
//0 表示修改成功
//1 表示有这个Section而没有Key,修改成功
//2 表示Section和Key都没有,修改成功
//-1表示修改失败
int SetValueByString(const char * SectionName ,const char * KeyName ,const char * Value);

protected:
// this function is used insided GetValueByString
char * ReadOneLine(long pos);
void TrimValue();

EFile m_IniFile;

//We use this to give the value to the callers
//so, programmer don't need to declare a new
//one and pass it to the function.
#define MAX_VALUE_SIZE 256
char m_Value[MAX_VALUE_SIZE];
};

#endif


lxigb 2002-02-26
  • 打赏
  • 举报
回复
配置文件不是用手在文本里面写写就行了????
然后在读取相应的值????
lxigb 2002-02-26
  • 打赏
  • 举报
回复
非常感谢大家!
非常!
我现在连配置文件是什么都不知道啊!!
说起来惭愧!!
能不能请大家从最简单的开始给我讲一讲啊!
谢了!
vc_boy 2002-02-26
  • 打赏
  • 举报
回复
INI文件格式:

[Test]
TestName = XXXXX ;这是一个测试

读写可以用API
GetPrivateProfileString、WritePrivateProfileString一类的函数,很容易。
如要取出TestName的值可用:
char m_V[255];
GetPrivateProfileString("Test","TestValue","Err",m_V,255,"Myini.ini")
strip 2002-02-26
  • 打赏
  • 举报
回复
CArrayPP: 我从mfc src里面抠出来的,这样就不依赖于mfc了
StrPP: 使用的是StrPP310,你可以从www.simtel.net下载
strip 2002-02-26
  • 打赏
  • 举报
回复
#ifndef CINI
#define CINI

//HOWTO:
//int main(int argc, char* argv[])
//{
// CIniFile ini;
// ini.ReadFile( _T("test.ini") );
// StrPP username = ini.QueryKeyValue( _T("general"), _T("UserName") );
// StrPP password = ini.QueryKeyValue( _T("ftp"), _T("Password") );
// MYTRACE( _T("\nUserName=>>%s<<"), (LPCTSTR)username );
// MYTRACE( _T("\npassword=>>%s<<\n"), (LPCTSTR)password );
// return 0;
//}

#include <windows.h>
#include "STR.h"
#include "ArrayPP.h"

typedef struct
{
StrPP Key;
StrPP Value;
} KEY_STRUCT, *LPKEY_STRUCT;

class CSection
{
public:
StrPP m_strName;
CArrayPP <KEY_STRUCT, KEY_STRUCT> m_arKeys;
public:
CSection( StrPP strName )
{
m_strName = strName;
}
~CSection()
{
}
StrPP GetName()
{
return m_strName;
}
long GetKeyCount()
{
return m_arKeys.GetSize();
}
KEY_STRUCT GetKey(long lIndex)
{
if( lIndex >=0 && lIndex < GetKeyCount() )
return m_arKeys[lIndex];
else
return m_arKeys[0];
}
StrPP QueryKeyValue( StrPP key )
{
for(int i=0; i<m_arKeys.GetSize(); i++)
{
if( m_arKeys[i].Key == key )
{
return m_arKeys[i].Value;
}
}
return STR_NULL;
}
void SetKeyValue( StrPP key, StrPP value )
{
for(int i=0; i<m_arKeys.GetSize(); i++)
{
if( m_arKeys[i].Key == key )
{
m_arKeys[i].Value = value;
return;
}
}
AddKey( key, value );
}
void AddKey( StrPP name, StrPP value )
{
KEY_STRUCT key;
key.Key = name;
key.Value = value;
m_arKeys.Add( key );
}
};
typedef CSection* CSectionPtr;

#define INIFILE_READ_BUFFER_SIZE 1024
class CIniFile
{
private:
CArrayPP <CSectionPtr, CSectionPtr> m_arSections;
public:
StrPP QueryKeyValue( StrPP strSectionName, StrPP strKeyName )
{
CSectionPtr section;
for(int i=0; i<m_arSections.GetSize(); i++)
{
section = m_arSections[i];
if( section->m_strName == strSectionName )
return section->QueryKeyValue( strKeyName );
}
return STR_NULL;
}
void SetKeyValue( StrPP strSessionName, StrPP strKeyName, StrPP strValue )
{
CSectionPtr section;
for(int i=0; i<m_arSections.GetSize(); i++)
{
section = m_arSections[i];
if( section->m_strName == strSessionName )
{
section->SetKeyValue( strKeyName, strValue );
return;
}
}
section = new CSection( strSessionName );
section->SetKeyValue( strKeyName, strValue );
AddSection( section );
}
void AddSection( CSectionPtr section )
{
m_arSections.Add( section );
}
CIniFile()
{
}
~CIniFile()
{
CSectionPtr section;
for(int i=0; i<m_arSections.GetSize(); i++)
{
section = m_arSections[i];
delete section;
}
}
long ReadFile( StrPP strFileName )
{
FILE* iniFile = _tfopen( strFileName, _T("r") );
if( iniFile != NULL)
{
char cInputBuffer[INIFILE_READ_BUFFER_SIZE];
char* line = NULL;
long lCount = 0;
StrPP strProcess;
CSectionPtr CurrentSession = NULL;

fseek( iniFile, SEEK_SET, 0);
lCount = 0;
do{
line = fgets( cInputBuffer, INIFILE_READ_BUFFER_SIZE, iniFile );
if( line != NULL )
{
#ifdef _UNICODE
LPWSTR lpUnicode = NULL;
AnsiToUnicode( cInputBuffer, &lpUnicode );
strProcess = lpUnicode;
SAFE_DELETE(lpUnicode);
#else
strProcess = cInputBuffer;
#endif
strProcess.Trim();
// MYTRACE( _T("\n%s"), (LPCTSTR)strProcess );
int length = strProcess.Length();
if( ( strProcess.SubStr(0, 1) == _T("[") ) && ( strProcess.SubStr(length-1, 1) == _T("]") ) )
{
CurrentSession = new CSection( strProcess.SubStr(1, length-2) );
AddSection( CurrentSession );
}
else
{
int pos = strProcess.FindFirst( _T("=") );
if( pos > 0 ) // should not be the first char :-)
{
// add key to CurrentSession
if( CurrentSession != NULL )
{
CurrentSession->AddKey( strProcess.SubStr( 0, pos ), strProcess.SubStr( pos+1, length - pos) );
}
else
{
// just ignore in this version of code
}
}
else
{
}
}
}
}while( !feof(iniFile) && (line != NULL) );

fclose( iniFile );
iniFile = NULL;
}
return 0;
}
long WriteFile( StrPP strFileName )
{
FILE* iniFile = _tfopen( strFileName, _T("w") );
if( iniFile != NULL)
{
fseek( iniFile, SEEK_SET, 0);
CSectionPtr section;
for(int i=0; i<m_arSections.GetSize(); i++)
{
section = m_arSections[i];
fputs( "[", iniFile );
fputs( (LPCSTR)section->GetName(), iniFile );
fputs( "]\n", iniFile );
for(int j=0; j<section->GetKeyCount(); j++)
{
KEY_STRUCT key = section->GetKey(j);
char cBuffer[INIFILE_READ_BUFFER_SIZE];
cBuffer[0]=0;
strcat( cBuffer, key.Key );
strcat( cBuffer, "=" );
strcat( cBuffer, key.Value );
strcat( cBuffer, "\n" );
fputs( cBuffer, iniFile );

}
}

fclose( iniFile );
iniFile = NULL;
}
return 0;
}
};

#endif
shadows_ 2002-02-26
  • 打赏
  • 举报
回复
用ini文件
Windows提供了读ini文件的API支持
蒋晟 2002-02-26
  • 打赏
  • 举报
回复
用WINAPI比较方便,自己写及其麻烦:(
ForApply 2002-02-26
  • 打赏
  • 举报
回复
some.ini

[section]
key=value ; comment
ForApply 2002-02-26
  • 打赏
  • 举报
回复
不会吧,为什么,以前我用watcomC++的时候是没有办法必须自己写
现在用GetPrivate...不是很好的吗
vc_boy 2002-02-26
  • 打赏
  • 举报
回复
配置文件其实就是一个文本文件,只不过它有它自己的格式而已,其中的内容是保存你的程序中有关的设置。
idoloveyou 2002-02-26
  • 打赏
  • 举报
回复
WritePrivateProfileSection
WritePrivateProfileString

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

试试用AI创作助手写篇文章吧