讀取配置檔案(.ini)
配置設定檔案是Windows作業系統下的一種特殊化的ASCII檔案,以"ini"為檔案擴充名。通常應用程式可以擁有自己的配置設定檔案來存儲自己的狀态資訊,可以減少程式在初始化時讀取配置檔案時的資訊量,進而可以提高程式的啟動速度、提高應用程式和系統的性能,使程式更加靈活。
1、配置檔案格式 : ini檔案由節、鍵、值組成
Example:
[System] ----------------------------[Section] 節
host=***.**.**.*** ----------------[鍵=值] key=value
username=User
password=123456
[TestParam]
OrderSpeed=4000,10000,40000
OrderTotal=120000,300000,3000000
Order=10000
Version=version1
bakpath=/home/**
localpath=C:\Users\***
[picmerge]
network=eth1,eth2
IO=sda
2、讀取配置檔案:
GetPrivateProfileIntA() / GetPrivateProfileInt() 從私有初始化檔案擷取整型數值
GetPrivateProfileStringA() / GetPrivateProfileString()從私有初始化檔案中擷取字元串型值
WritePrivateProfileStringA() / WritePrivateProfileString() 寫字元串到私有初始化檔案
//MFC
CString localpath;
::GetPrivateProfileStringA("TestParam","localpath","NULL",localpath.GetBuffer(100),100,IniPath); //IniPath為配置檔案路徑
int order = GetPrivateProfileIntA("TestParam","Order",0,IniPath); //傳回值即為讀取到的參數
WritePrivateProfileStringA("TestParam",”Version","version",IniPath); //寫入字元串
localpath.ReleaseBuffer(); //在對GetBuffer傳回的指針使用之後需要先調用ReleaseBuffer,這樣才能使用其他CString的operations
/*********其它操作過程************/
//C++
LPTSTR localpath = new char[100];
GetPrivateProfileString("TestParam","localpath","NULL",localpath,100,IniPath); //擷取目标檔案夾路徑
/***********************其它處理過程,如CString strPath = localpath等********/
delete [] localpath; //new (LPTSTR)要在對其作其它處理後進行delete
//讀取整型和寫入參考MFC