哈哈!给你一个INI文件类,其中的小BUG,我已经修复了,但还是有一点,但不影响使用的!.h
--------------------------------
// IniFile.h: interface for the CIniFile class.
// Written by: Adam Clauss
// Email: [email protected]
// You may use this class/code as you wish in your programs. Feel free to distribute it, and
// email suggested changes to me.
//////////////////////////////////////////////////////////////////////#if !defined(AFX_INIFILE_H__D6BE0D97_13A8_11D4_A5D2_002078B03530__INCLUDED_)
#define AFX_INIFILE_H__D6BE0D97_13A8_11D4_A5D2_002078B03530__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//#include "stdafx.h"
#include <afxtempl.h>
class CIniFile
{
//all private variables
private: //stores pathname of ini file to read/write
CString path;
//all keys are of this time
struct key
{
//list of values in key
CArray<CString, CString> values; //corresponding list of value names
CArray<CString, CString> names;
}; //list of keys in ini
CArray<key, key> keys; //corresponding list of keynames
CArray<CString, CString> names;
//all private functions
private: //returns index of specified value, in the specified key, or -1 if not found
int FindValue(int keynum, CString valuename); //returns index of specified key, or -1 if not found
int FindKey(CString keyname);
//public variables
public: //will contain error info if one occurs
//ended up not using much, just in ReadFile and GetValue
CString error;
//public functions
public:
//default constructor
CIniFile(); //constructor, can specify pathname here instead of using SetPath later
CIniFile(CString inipath); //default destructor
virtual ~CIniFile(); //sets path of ini file to read and write from
void SetPath(CString newpath); //reads ini file specified using CIniFile::SetPath()
//returns true if successful, false otherwise
bool ReadFile(); //writes data stored in class to ini file
void WriteFile(); //deletes all stored ini data
void Reset(); //returns number of keys currently in the ini
int GetNumKeys(); //returns number of values stored for specified key
int GetNumValues(CString keyname); //gets value of [keyname] valuename =
//overloaded to return CString, int, and double,
//returns "", or 0 if key/value not found. Sets error member to show problem
CString GetValue(CString keyname, CString valuename,CString strDefault = "");
int GetValueI(CString keyname, CString valuename,int iDefault);
double GetValueF(CString keyname, CString valuename,double dDefault); //sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
bool SetValue(CString key, CString valuename, CString value, bool create = 1);
bool SetValueI(CString key, CString valuename, int value, bool create = 1);
bool SetValueF(CString key, CString valuename, double value, bool create = 1); //deletes specified value
//returns true if value existed and deleted, false otherwise
bool DeleteValue(CString keyname, CString valuename); //deletes specified key and all values contained within
//returns true if key existed and deleted, false otherwise
bool DeleteKey(CString keyname);
};#endif // !defined(AFX_INIFILE_H__D6BE0D97_13A8_11D4_A5D2_002078B03530__INCLUDED_)
--------------------------------
// IniFile.h: interface for the CIniFile class.
// Written by: Adam Clauss
// Email: [email protected]
// You may use this class/code as you wish in your programs. Feel free to distribute it, and
// email suggested changes to me.
//////////////////////////////////////////////////////////////////////#if !defined(AFX_INIFILE_H__D6BE0D97_13A8_11D4_A5D2_002078B03530__INCLUDED_)
#define AFX_INIFILE_H__D6BE0D97_13A8_11D4_A5D2_002078B03530__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//#include "stdafx.h"
#include <afxtempl.h>
class CIniFile
{
//all private variables
private: //stores pathname of ini file to read/write
CString path;
//all keys are of this time
struct key
{
//list of values in key
CArray<CString, CString> values; //corresponding list of value names
CArray<CString, CString> names;
}; //list of keys in ini
CArray<key, key> keys; //corresponding list of keynames
CArray<CString, CString> names;
//all private functions
private: //returns index of specified value, in the specified key, or -1 if not found
int FindValue(int keynum, CString valuename); //returns index of specified key, or -1 if not found
int FindKey(CString keyname);
//public variables
public: //will contain error info if one occurs
//ended up not using much, just in ReadFile and GetValue
CString error;
//public functions
public:
//default constructor
CIniFile(); //constructor, can specify pathname here instead of using SetPath later
CIniFile(CString inipath); //default destructor
virtual ~CIniFile(); //sets path of ini file to read and write from
void SetPath(CString newpath); //reads ini file specified using CIniFile::SetPath()
//returns true if successful, false otherwise
bool ReadFile(); //writes data stored in class to ini file
void WriteFile(); //deletes all stored ini data
void Reset(); //returns number of keys currently in the ini
int GetNumKeys(); //returns number of values stored for specified key
int GetNumValues(CString keyname); //gets value of [keyname] valuename =
//overloaded to return CString, int, and double,
//returns "", or 0 if key/value not found. Sets error member to show problem
CString GetValue(CString keyname, CString valuename,CString strDefault = "");
int GetValueI(CString keyname, CString valuename,int iDefault);
double GetValueF(CString keyname, CString valuename,double dDefault); //sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
bool SetValue(CString key, CString valuename, CString value, bool create = 1);
bool SetValueI(CString key, CString valuename, int value, bool create = 1);
bool SetValueF(CString key, CString valuename, double value, bool create = 1); //deletes specified value
//returns true if value existed and deleted, false otherwise
bool DeleteValue(CString keyname, CString valuename); //deletes specified key and all values contained within
//returns true if key existed and deleted, false otherwise
bool DeleteKey(CString keyname);
};#endif // !defined(AFX_INIFILE_H__D6BE0D97_13A8_11D4_A5D2_002078B03530__INCLUDED_)
解决方案 »
- 熟悉detours的好心人帮帮我
- 一个比较容易的COM入门级问题
- 在.net中的ATL工程中如何添加自定义的数据类型?
- QQ那种拉窗帘的效果怎么做的呢?
- 多线程问题:作了一个基于对话框的程序,里边放了一个ListBox控件,然后从CWinThread派生CMyThread类,重载Run函数,
- 如果我想靠它生活
- 我在win7的64位虚拟机上安装了VS2008,然后向编译64位的程序
- 哪儿有问题,设置背景色没效果
- C1010: 错误
- vs2013/mfc 子对话框里运行列表控件出错
- 请问哪里有关于map<*,*>的中文使用说明,我不懂类似这样的东西,好像叫模板
- 各位大虾,我编了一个odbc的数据库
-----------------------------------------------
// IniFile.cpp: implementation of the CIniFile class.
// Written by: Adam Clauss
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "IniFile.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif/////////////////////////////////////////////////////////////////////
// Construction/Destruction
///////////////////////////////////////////////////////////////////////default constructor
CIniFile::CIniFile()
{
}//constructor, can specify pathname here instead of using SetPath later
CIniFile::CIniFile(CString inipath)
{
path = inipath;
}//default destructor
CIniFile::~CIniFile()
{}/////////////////////////////////////////////////////////////////////
// Public Functions
///////////////////////////////////////////////////////////////////////sets path of ini file to read and write from
void CIniFile::SetPath(CString newpath)
{
path = newpath;
}//reads ini file specified using CIniFile::SetPath()
//returns true if successful, false otherwise
bool CIniFile::ReadFile()
{
CStdioFile inifile;
CString readinfo;
if(inifile.Open(path,CFile::modeRead) == 0)
{
error = "Unable to open ini file.";
return 0;
}
int curkey = -1, curval = -1;
CString keyname, valuename, value;
CString temp;
while (inifile.ReadString(readinfo))
{
readinfo.TrimLeft();
readinfo.TrimRight();
if(readinfo.Left(2)== "//")
continue;
if (readinfo != "")
{
if (readinfo[0] == '[' && readinfo[readinfo.GetLength()-1] == ']') //if a section heading
{
keyname = readinfo;
keyname.TrimLeft('[');
keyname.TrimRight(']');
}
else //if a value
{
valuename = readinfo.Left(readinfo.Find("="));
value = readinfo.Right(readinfo.GetLength()-valuename.GetLength()-1);
SetValue(keyname,valuename,value);
}
}
}
inifile.Close();
return 1;
}//writes data stored in class to ini file
void CIniFile::WriteFile()
{
CStdioFile inifile;
CString tmpStr;
if(inifile.Open(path,CFile::modeCreate|CFile::modeWrite) == 0)
{
error = "Unable to Create or Write ini file.";
return ;
}
for (int keynum = 0; keynum <= names.GetUpperBound(); keynum++)
{
if (keys[keynum].names.GetSize() != 0)
{
tmpStr = "[" + names[keynum];
tmpStr += "]\n";
inifile.WriteString(tmpStr); for (int valuenum = 0; valuenum <= keys[keynum].names.GetUpperBound(); valuenum++)
{
tmpStr = keys[keynum].names[valuenum] + "=";
tmpStr += keys[keynum].values[valuenum] + "\n";
inifile.WriteString(tmpStr);
// if (valuenum != keys[keynum].names.GetUpperBound())
// inifile.WriteString("\r\n");
// else
// if (keynum < names.GetSize())
// inifile.WriteString("\r\n");
}
if (keynum < names.GetSize())
inifile.WriteString("\n");
}
}
inifile.Close();
}//deletes all stored ini data
void CIniFile::Reset()
{
keys.SetSize(0);
names.SetSize(0);
}//returns number of keys currently in the ini
int CIniFile::GetNumKeys()
{
return keys.GetSize();
}//returns number of values stored for specified key, or -1 if key found
int CIniFile::GetNumValues(CString keyname)
{
int keynum = FindKey(keyname);
if (keynum == -1)
return -1;
else
return keys[keynum].names.GetSize();
}//gets value of [keyname] valuename =
//overloaded to return CString, int, and double
CString CIniFile::GetValue(CString keyname, CString valuename,CString strDefault)
{
int keynum = FindKey(keyname), valuenum = FindValue(keynum,valuename); if (keynum == -1)
{
error = "Unable to locate specified key.";
return strDefault;
} if (valuenum == -1)
{
error = "Unable to locate specified value.";
return strDefault;
}
return keys[keynum].values[valuenum];
}//gets value of [keyname] valuename =
//overloaded to return CString, int, and double
int CIniFile::GetValueI(CString keyname, CString valuename,int iDefault)
{
CString str = GetValue(keyname,valuename);
if(str == "")
return iDefault;
else
return atoi(str);
}//gets value of [keyname] valuename =
//overloaded to return CString, int, and double
double CIniFile::GetValueF(CString keyname, CString valuename,double dDefault)
{
CString str = GetValue(keyname,valuename);
if(str == "")
return dDefault;
else
return atof(str);
}//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
bool CIniFile::SetValue(CString keyname, CString valuename, CString value, bool create)
{
int keynum = FindKey(keyname), valuenum = 0;
//find key
if (keynum == -1) //if key doesn't exist
{
if (!create) //and user does not want to create it,
return 0; //stop entering this key
names.SetSize(names.GetSize()+1);
keys.SetSize(keys.GetSize()+1);
keynum = names.GetSize()-1;
names[keynum] = keyname;
} //find value
valuenum = FindValue(keynum,valuename);
if (valuenum == -1)
{
if (!create)
return 0;
keys[keynum].names.SetSize(keys[keynum].names.GetSize()+1);
keys[keynum].values.SetSize(keys[keynum].names.GetSize()+1);
valuenum = keys[keynum].names.GetSize()-1;
keys[keynum].names[valuenum] = valuename;
}
keys[keynum].values[valuenum] = value;
return 1;
}//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
bool CIniFile::SetValueI(CString keyname, CString valuename, int value, bool create)
{
CString temp;
temp.Format("%d",value);
return SetValue(keyname, valuename, temp, create);
}//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
bool CIniFile::SetValueF(CString keyname, CString valuename, double value, bool create)
{
CString temp;
temp.Format("%f",value);
return SetValue(keyname, valuename, temp, create);
}//deletes specified value
//returns true if value existed and deleted, false otherwise
bool CIniFile::DeleteValue(CString keyname, CString valuename)
{
int keynum = FindKey(keyname), valuenum = FindValue(keynum,valuename);
if (keynum == -1 || valuenum == -1)
return 0; keys[keynum].names.RemoveAt(valuenum);
keys[keynum].values.RemoveAt(valuenum);
return 1;
}//deletes specified key and all values contained within
//returns true if key existed and deleted, false otherwise
bool CIniFile::DeleteKey(CString keyname)
{
int keynum = FindKey(keyname);
if (keynum == -1)
return 0;
keys.RemoveAt(keynum);
names.RemoveAt(keynum);
return 1;
}/////////////////////////////////////////////////////////////////////
// Private Functions
///////////////////////////////////////////////////////////////////////returns index of specified key, or -1 if not found
int CIniFile::FindKey(CString keyname)
{
int keynum = 0;
while ( keynum < keys.GetSize() && names[keynum] != keyname)
keynum++;
if (keynum == keys.GetSize())
return -1;
return keynum;
}//returns index of specified value, in the specified key, or -1 if not found
int CIniFile::FindValue(int keynum, CString valuename)
{
if (keynum == -1)
return -1;
int valuenum = 0;
while (valuenum < keys[keynum].names.GetSize() && keys[keynum].names[valuenum] != valuename)
valuenum++;
if (valuenum == keys[keynum].names.GetSize())
return -1;
return valuenum;
}
-----------------------------------------------
// IniFile.cpp: implementation of the CIniFile class.
// Written by: Adam Clauss
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "IniFile.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif/////////////////////////////////////////////////////////////////////
// Construction/Destruction
///////////////////////////////////////////////////////////////////////default constructor
CIniFile::CIniFile()
{
}//constructor, can specify pathname here instead of using SetPath later
CIniFile::CIniFile(CString inipath)
{
path = inipath;
}//default destructor
CIniFile::~CIniFile()
{}/////////////////////////////////////////////////////////////////////
// Public Functions
///////////////////////////////////////////////////////////////////////sets path of ini file to read and write from
void CIniFile::SetPath(CString newpath)
{
path = newpath;
}//reads ini file specified using CIniFile::SetPath()
//returns true if successful, false otherwise
bool CIniFile::ReadFile()
{
CStdioFile inifile;
CString readinfo;
if(inifile.Open(path,CFile::modeRead) == 0)
{
error = "Unable to open ini file.";
return 0;
}
int curkey = -1, curval = -1;
CString keyname, valuename, value;
CString temp;
while (inifile.ReadString(readinfo))
{
readinfo.TrimLeft();
readinfo.TrimRight();
if(readinfo.Left(2)== "//")
continue;
if (readinfo != "")
{
if (readinfo[0] == '[' && readinfo[readinfo.GetLength()-1] == ']') //if a section heading
{
keyname = readinfo;
keyname.TrimLeft('[');
keyname.TrimRight(']');
}
else //if a value
{
valuename = readinfo.Left(readinfo.Find("="));
value = readinfo.Right(readinfo.GetLength()-valuename.GetLength()-1);
SetValue(keyname,valuename,value);
}
}
}
inifile.Close();
return 1;
}//writes data stored in class to ini file
void CIniFile::WriteFile()
{
CStdioFile inifile;
CString tmpStr;
if(inifile.Open(path,CFile::modeCreate|CFile::modeWrite) == 0)
{
error = "Unable to Create or Write ini file.";
return ;
}
for (int keynum = 0; keynum <= names.GetUpperBound(); keynum++)
{
if (keys[keynum].names.GetSize() != 0)
{
tmpStr = "[" + names[keynum];
tmpStr += "]\n";
inifile.WriteString(tmpStr); for (int valuenum = 0; valuenum <= keys[keynum].names.GetUpperBound(); valuenum++)
{
tmpStr = keys[keynum].names[valuenum] + "=";
tmpStr += keys[keynum].values[valuenum] + "\n";
inifile.WriteString(tmpStr);
// if (valuenum != keys[keynum].names.GetUpperBound())
// inifile.WriteString("\r\n");
// else
// if (keynum < names.GetSize())
// inifile.WriteString("\r\n");
}
if (keynum < names.GetSize())
inifile.WriteString("\n");
}
}
inifile.Close();
}//deletes all stored ini data
void CIniFile::Reset()
{
keys.SetSize(0);
names.SetSize(0);
}//returns number of keys currently in the ini
int CIniFile::GetNumKeys()
{
return keys.GetSize();
}//returns number of values stored for specified key, or -1 if key found
int CIniFile::GetNumValues(CString keyname)
{
int keynum = FindKey(keyname);
if (keynum == -1)
return -1;
else
return keys[keynum].names.GetSize();
}//gets value of [keyname] valuename =
//overloaded to return CString, int, and double
CString CIniFile::GetValue(CString keyname, CString valuename,CString strDefault)
{
int keynum = FindKey(keyname), valuenum = FindValue(keynum,valuename); if (keynum == -1)
{
error = "Unable to locate specified key.";
return strDefault;
} if (valuenum == -1)
{
error = "Unable to locate specified value.";
return strDefault;
}
return keys[keynum].values[valuenum];
}//gets value of [keyname] valuename =
//overloaded to return CString, int, and double
int CIniFile::GetValueI(CString keyname, CString valuename,int iDefault)
{
CString str = GetValue(keyname,valuename);
if(str == "")
return iDefault;
else
return atoi(str);
}//gets value of [keyname] valuename =
//overloaded to return CString, int, and double
double CIniFile::GetValueF(CString keyname, CString valuename,double dDefault)
{
CString str = GetValue(keyname,valuename);
if(str == "")
return dDefault;
else
return atof(str);
}//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
bool CIniFile::SetValue(CString keyname, CString valuename, CString value, bool create)
{
int keynum = FindKey(keyname), valuenum = 0;
//find key
if (keynum == -1) //if key doesn't exist
{
if (!create) //and user does not want to create it,
return 0; //stop entering this key
names.SetSize(names.GetSize()+1);
keys.SetSize(keys.GetSize()+1);
keynum = names.GetSize()-1;
names[keynum] = keyname;
} //find value
valuenum = FindValue(keynum,valuename);
if (valuenum == -1)
{
if (!create)
return 0;
keys[keynum].names.SetSize(keys[keynum].names.GetSize()+1);
keys[keynum].values.SetSize(keys[keynum].names.GetSize()+1);
valuenum = keys[keynum].names.GetSize()-1;
keys[keynum].names[valuenum] = valuename;
}
keys[keynum].values[valuenum] = value;
return 1;
}//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
bool CIniFile::SetValueI(CString keyname, CString valuename, int value, bool create)
{
CString temp;
temp.Format("%d",value);
return SetValue(keyname, valuename, temp, create);
}//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
bool CIniFile::SetValueF(CString keyname, CString valuename, double value, bool create)
{
CString temp;
temp.Format("%f",value);
return SetValue(keyname, valuename, temp, create);
}//deletes specified value
//returns true if value existed and deleted, false otherwise
bool CIniFile::DeleteValue(CString keyname, CString valuename)
{
int keynum = FindKey(keyname), valuenum = FindValue(keynum,valuename);
if (keynum == -1 || valuenum == -1)
return 0; keys[keynum].names.RemoveAt(valuenum);
keys[keynum].values.RemoveAt(valuenum);
return 1;
}//deletes specified key and all values contained within
//returns true if key existed and deleted, false otherwise
bool CIniFile::DeleteKey(CString keyname)
{
int keynum = FindKey(keyname);
if (keynum == -1)
return 0;
keys.RemoveAt(keynum);
names.RemoveAt(keynum);
return 1;
}/////////////////////////////////////////////////////////////////////
// Private Functions
///////////////////////////////////////////////////////////////////////returns index of specified key, or -1 if not found
int CIniFile::FindKey(CString keyname)
{
int keynum = 0;
while ( keynum < keys.GetSize() && names[keynum] != keyname)
keynum++;
if (keynum == keys.GetSize())
return -1;
return keynum;
}//returns index of specified value, in the specified key, or -1 if not found
int CIniFile::FindValue(int keynum, CString valuename)
{
if (keynum == -1)
return -1;
int valuenum = 0;
while (valuenum < keys[keynum].names.GetSize() && keys[keynum].names[valuenum] != valuename)
valuenum++;
if (valuenum == keys[keynum].names.GetSize())
return -1;
return valuenum;
}
[email protected]
LOGFONT lf;
...
CString fontini;
fontini.Format("%u",lf);
WritePrivateProfileString([**],"font",fontini,".\\myfont.ini");
...
在ini文件中font=4294967281;
现在的问题是通过 GetPrivateProfileString读取font的值后怎样转换为LOGFONT???
memset(&f,0,sizeof(LOGFONT));
memcpy(&f,pvalue,sizeof(LOGFONT));