C/C++讀取.ini的設定檔__C++

來源:互聯網
上載者:User

 記錄一下,讀取.ini設定檔的方法。

實際中,一般先獲得當前程式所在路徑(或者放在特定的設定檔目錄)

GetModuleFileName

解釋下這句:strrchr(cfgpath, '\\')[0] = '\0';

strrchr(cfgpath, '\\')返迴路徑中'\'最後出現的位置,strrchr(cfgpath, '\\')[0]則把該位置改為'\0'。之後strcat後,就得到了設定檔的絕對位址。

GetPrivateProfileStringA(這裡是字串,還有其他類型的)就是得到設定檔相應值得函數了,一定要是絕對路徑。

char cfgpath[MAX_PATH];int bDenoise = 0;if (GetModuleFileNameA(NULL, cfgpath, MAX_PATH) > 0){    strrchr(cfgpath, '\\')[0] = '\0';    strcat(cfgpath, "\\config.ini");    char tmp[8];    GetPrivateProfileStringA("denoise", "denoise", "0", tmp, 8, cfgpath);    bDenoise = atoi(tmp);}

還有一種寬字元的寫法:

TCHAR iniFullPath[MAX_PATH];if (GetModuleFileName(NULL, iniFullPath, MAX_PATH) > 0){    (_tcsrchr(iniFullPath, _T('\\')))[1] = 0;    wstring strPath = iniFullPath;    strPath = strPath + _T("config.ini");    bDenoise = GetPrivateProfileInt(L"denoise", L"denoise", 0, strPath.c_str());}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.