哈哈!给你一个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_)

解决方案 »

  1.   

    .cpp
    -----------------------------------------------
    // 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;
    }
      

  2.   

    .cpp
    -----------------------------------------------
    // 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;
    }
      

  3.   

    嘿嘿,楼上的老兄,冲你这个态度,值得交个朋友?
    [email protected]
      

  4.   

    没有人会啊!!!!!!!!!!!!!!!!!
    LOGFONT lf;
    ...
    CString fontini;
    fontini.Format("%u",lf);
    WritePrivateProfileString([**],"font",fontini,".\\myfont.ini");
    ...
    在ini文件中font=4294967281;
    现在的问题是通过 GetPrivateProfileString读取font的值后怎样转换为LOGFONT???
      

  5.   

    LOGFONT f
    memset(&f,0,sizeof(LOGFONT));
    memcpy(&f,pvalue,sizeof(LOGFONT));