c++ - Boost Property Tree Json Read for file containing LPWSTR -
i have code supposed write content in file using writefile. type of contents written in file lpwstr ie wchar_t *. file write ip, ssl , compression. consider following code:
#include <windows.h> #include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> int main() { lpwstr ip = null; lpwstr ssl = null; lpwstr comp = null; wchar_t buffer[300]; handle hfile; bool berrorflag; dword dwbytestowrite = 0; //(dword)strlen(buffer); dword dwbyteswritten = 0; if(ip == null || wcslen(ip) == 0 ) { ip = l"127.0.0.1"; } if(ssl == null || wcslen(ssl) == 0) { ssl = l"false"; } if(comp == null || wcslen(comp) == 0 ) { comp = l"true"; } wsprintf(buffer, l"{\n\"ip\": \"%ls\",\n\"ssl\": \"%ls\",\n\"compression\":\"%ls\"\n}",ip,ssl,comp); //swprintf(buffer, 150, l"{\n\"ipaddress\": \"%ls\",\n\"ssl\": \"%ls\",\n\"compression\":\"%ls\"\n}",ip,ssl,comp); std::wcout << buffer << std::endl; dwbytestowrite = (wcslen(buffer)) * sizeof(wchar_t); hfile = createfile(l"c://somefolder//some_config.config", // name of write generic_write, // open writing 0, // not share null, // default security create_always, // create new file file_attribute_normal, // normal file null); berrorflag = writefile( hfile, // open file handle buffer, // start of data write dwbytestowrite, // number of bytes write &dwbyteswritten, // number of bytes written null); // no overlapped structure closehandle(hfile); boost::property_tree::ptree pt; try { boost::property_tree::read_json("c://somefolder//some_config.config", pt); } catch(std::exception &e) { std::cout << e.what(); } try { std::cout << pt.get<std::string>("ip"); } catch(std::exception &e) { std::cout << e.what(); } } the contents of file have
{ "ip": "127.0.0.1", "ssl": "false", "compression":"true" } but using read_json fails , gives error:
c://somefolder//some_config.config(1): expected object name no such node (ip) what wrong in code? why can't read_json reads file written? if using writefile incorrectly, please correct me. thanks.
you want use wptree:
boost::property_tree::wptree pt; boost::property_tree::read_json("c://somefolder//some_config.config", pt); std::wcout << pt.get<std::wstring>(l"ip"); note l"ip" , wstring there.
sidenote: need const versions of lpwstr pointers (lpcwstr? guess) if you're assigning string literals
Comments
Post a Comment