반응형

INI 파일 라이브러리(Leksys' INI Parser https://github.com/Lek-sys/LeksysINI )를 사용하여 INI 파일을 생성하고 값을 읽고 쓰는 예제입니다. 

깃허브에서 iniparser.hpp 파일만 가져오면 예제를 동작시킬 수 있습니다. 


2021. 01. 24 최초작성




#include <iostream>
#include <windows.h>
#include <atlconv.h>
#include "iniparser.hpp"


std::string get_current_path()
{
wchar_t path[MAX_PATH] = { 0 };
GetModuleFileName(NULL, path, MAX_PATH);

USES_CONVERSION;
std::string str = W2A(path);
str = str.substr(0, str.find_last_of("\\/"));

return str;
}

int main()
{
std::string path = get_current_path();
std::string fullpath = path + "\\config.ini";


INI::File ft;

ft.GetSection("FTP")->SetValue("ip", "192.168.0.100");
ft.GetSection("FTP")->SetValue("port", "21");
ft.GetSection("FTP")->SetValue("id", "webnautes");
ft.GetSection("FTP")->SetValue("password", "1234");
ft.Save(fullpath);


if (!ft.Load(fullpath))
{
std::cout << "설정파일이 존재 하지 않습니다.\n" << std::endl;
}

std::string ret;

// 설정값을 못읽었을 경우 디폴트값으로 -1 지정
std::string ip = ft.GetSection("FTP")->GetValue("ip", -1).AsString();
std::string port = ft.GetSection("FTP")->GetValue("port", -1).AsString();
std::string id = ft.GetSection("FTP")->GetValue("id", -1).AsString();
std::string password = ft.GetSection("FTP")->GetValue("password", -1).AsString();

std::cout << "ip = " << ip << std::endl;
std::cout << "port = " << port << std::endl;
std::cout << "id = " << id << std::endl;
std::cout << "password = " << password << std::endl;

}



ip = 192.168.0.100

port = 21

id = webnautes

password = 1234




실행파일이 있는 위치에 INI 파일이 다음 내용으로 생성됩니다. 





반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts