C++読み書きini構成ファイルの実現過程の詳細
WindowsのVCの下で
iniファイルを読む
例えば、D:\test.iniファイルで
[Font]
name=宋体
size=12 pt
カラー=RGB(255,0,0)
上の=番の両側にスペースを入れてもいいです。入れなくてもいいです。
Get Private ProfileInt()とGet ProfileStringを使う()
WritePrivate Profile String関数は、integerが書かれていないので、stringに変えて書き込めます。
Remarks
If the lp File Name parameter does not contain a full path and file name for the file,WriteProfile String searches the Windows directory for the file.If the file dot exist,this function creates the firectory Windows。
If lp FileName contains a full path and file name and the file does not exist,WriteProfile String creates the file.The specified directory must alady exist.
WritePrivate ProfileSection:
Remarks
The data in the buffer pointed to by the lp String parameter consists of one or more null-terminated stings,followed by a final null character.Each string has the following form:
key=string
The WritePrivate Profile Section function is not case-sensitive;the string pointed to by the lpname parameter can be a cobination of upercase and lowercase letters.
Ifのsection name matches the string pointed to by the lpname parameter,WritePrivate ProfileSection creates the section the section of the specifition file and initiazation file and initializes the nectiorizes the sectiories specine the sprever the specinevarever the
WritePrivate ProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter.The function does notempreet coreetif the new names appar in a different order from the old names,any comments assicated with preexisting keys and values in the initiazation file probable will probasciated with corect keys and values.
This operation is atomic;no operation s that read from or write to the specifile are allowed while the information is being written.
例:
あるアクションのすべてのkey=valueを取得します。
mapGet KeysValues(LPCTSTR szSection、LPCTSTR SzIni FilePath)
iniファイルのすべてのアクション名を取得します。
vectorGet Sections Names(LPCTSTR szIni FilePath)
iniファイルを読む
例えば、D:\test.iniファイルで
[Font]
name=宋体
size=12 pt
カラー=RGB(255,0,0)
上の=番の両側にスペースを入れてもいいです。入れなくてもいいです。
Get Private ProfileInt()とGet ProfileStringを使う()
[section]
key=string
.
.
integer
UINT GetPrivateProfileInt(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
INT nDefault, // return value if key name not found
LPCTSTR lpFileName // initialization file name
);
:lpAppName lpKeyName , integer <0, 0。lpFileName , C:\windows\
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
LPCTSTR lpDefault, // default string
LPTSTR lpReturnedString, // destination buffer
DWORD nSize, // size of destination buffer
LPCTSTR lpFileName // initialization file name
);
:lpAppName lpKeyName , lpAppName NULL,lpReturnedString ini , lpKeyName=NULL, lpReturnedString 。lpFileName , C:\windows\,
: lpReturnedString , NULL 。 lpReturnedString , , nSize-1( lpAppName lpKeyName NULL, nSize-2)
keys values
DWORD GetPrivateProfileSection(
LPCTSTR lpAppName, // section name
LPTSTR lpReturnedString, // return buffer
DWORD nSize, // size of return buffer
LPCTSTR lpFileName // initialization file name
);
retrieves the names of all sections in an initialization file.
DWORD GetPrivateProfileSectionNames(
LPTSTR lpszReturnBuffer, // return buffer
DWORD nSize, // size of return buffer
LPCTSTR lpFileName // initialization file name
);
,GetPrivateProfileString(NULL,NULL,lpszReturnedBuffer,nSize,lpFileName)
例:
/* test.ini "=" ,
[Font]
name=
size= 12pt
color = RGB(255,0,0)
[Layout]
[Body]
*/
CString strCfgPath = _T("D:\\test.ini"); // :'\\'
LPCTSTR lpszSection = _T("Font");
int n = GetPrivateProfileInt(_T("FONT"), _T("size"), 9, strCfgPath);//n=12
CString str;
GetPrivateProfileString(lpszSection, _T("size"), _T("9pt"), str.GetBuffer(MAX_PATH), MAX_PATH, strCfgPath);
str.ReleaseBuffer();//str="12pt"
TCHAR buf[200] = { 0 };
int nSize = sizeof(buf) / sizeof(buf[0]);
GetPrivateProfileString(lpszSection, NULL, _T(""), buf, nSize, strCfgPath);
//buf: "name\0size\0color\0\0"
memset(buf, 0, sizeof(buf));
GetPrivateProfileString(NULL, _T("size"), _T(""), buf, nSize, strCfgPath);// Section,_T("size") , NULL
// GetPrivateProfileString(NULL, NULL, _T(""), buf, nSize, strCfgPath);
//buf: "Font\0Layout\0Body\0\0"
memset(buf, 0, sizeof(buf));
GetPrivateProfileSection(lpszSection, buf, nSize, strCfgPath);
//buf: "name= \0size=12pt\0color=RGB(255,0,0)\0\0" “=”
memset(buf, 0, sizeof(buf));
GetPrivateProfileSectionNames(buf, nSize, strCfgPath);// GetPrivateProfileString(NULL, NULL, _T(""), buf, nSize, strCfgPath);
//buf: "Font\0Layout\0Body\0\0"
iniファイルを書くWritePrivate Profile String関数は、integerが書かれていないので、stringに変えて書き込めます。
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
LPCTSTR lpString, // string to add
LPCTSTR lpFileName // initialization file
);
The WritePrivateProfileSection function replaces the keys and values for the specified section in an initialization file.
BOOL WritePrivateProfileSection(
LPCTSTR lpAppName, // section name
LPCTSTR lpString, // data
LPCTSTR lpFileName // file name
);
WritePrivate Profile String:Remarks
If the lp File Name parameter does not contain a full path and file name for the file,WriteProfile String searches the Windows directory for the file.If the file dot exist,this function creates the firectory Windows。
If lp FileName contains a full path and file name and the file does not exist,WriteProfile String creates the file.The specified directory must alady exist.
WritePrivate ProfileSection:
Remarks
The data in the buffer pointed to by the lp String parameter consists of one or more null-terminated stings,followed by a final null character.Each string has the following form:
key=string
The WritePrivate Profile Section function is not case-sensitive;the string pointed to by the lpname parameter can be a cobination of upercase and lowercase letters.
Ifのsection name matches the string pointed to by the lpname parameter,WritePrivate ProfileSection creates the section the section of the specifition file and initiazation file and initializes the nectiorizes the sectiories specine the sprever the specinevarever the
WritePrivate ProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter.The function does notempreet coreetif the new names appar in a different order from the old names,any comments assicated with preexisting keys and values in the initiazation file probable will probasciated with corect keys and values.
This operation is atomic;no operation s that read from or write to the specifile are allowed while the information is being written.
例:
WritePrivateProfileString(_T("Layout"), _T("left"), _T("100"), strCfgPath);
WritePrivateProfileString(_T("Layout"), _T("top"), _T("80"), strCfgPath);
// Section, [Layout] Keys=Value
WritePrivateProfileSection(_T("Layout"), NULL, strCfgPath);
// Section, [Layout] Keys=Value, [Layout]
WritePrivateProfileSection(_T("Layout"), _T(""), strCfgPath);
// :WritePrivateProfileSection(NULL, NULL, strCfgPath); , Section NULL
自分のパッケージの関数:あるアクションのすべてのkey=valueを取得します。
map
iniファイルのすべてのアクション名を取得します。
vector
#include <vector>
#include <map>
using std::vector;
using std::map;
// ini Section
vector<CString> GetSectionsNames(LPCTSTR szIniFilePath)
{
vector<CString> vRet;
TCHAR buf[2048] = { 0 };
long nSize = sizeof(buf) / sizeof(buf[0]);
::GetPrivateProfileSectionNames(buf, nSize, szIniFilePath);
TCHAR *p, *q;
p = q = buf;
while (*p)// '\0' != *p
{
while (*q)
{
++q;
}
CString str(p, q - p);
vRet.push_back(str);
p = q + 1;
q = q + 1;
}
return vRet;
}
// Section key=value
map<CString, CString> GetKeysValues(LPCTSTR szSection, LPCTSTR szIniFilePath)
{
map<CString,CString> mapRet;
TCHAR buf[2048] = { 0 };
long nSize = sizeof(buf) / sizeof(buf[0]);
GetPrivateProfileSection(szSection, buf, nSize, szIniFilePath);
TCHAR* p = buf;
TCHAR* q = buf;
while (*p)
{
CString strKey, strValue;
while(*q)
{
if (_T('=') == *q)
{
strKey = CString(p, q - p);
p = q + 1;
}
++q;
}
strValue = CString(p, q - p);
mapRet.insert(std::make_pair(strKey, strValue));
p = q + 1;
q = q + 1;
}
return mapRet;
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。