Monday, 15 March 2010

c++ - Boost Property Tree Json Read for file containing LPWSTR -


I have some code that should write content in the file using WriteFile . The type of content written in the file is LPWSTR i.e. wchar_t * . The file will be written as ip , ssl and compression . Consider the following code:

  #include & lt; Windows.h & gt; # Include & lt; Iostream & gt; # Include & lt; Boost / property_tree / ptree.hpp & gt; # Include & lt; Boost / property_tree / json_parser.hpp & gt; Int main () {LPWSTR ip = NULL; LPDbluster SSL = Faucet; LPWT Comp = Null; Wchar_t buffer [300]; Handle hfile; BOOL Barflag; DWORD dwBytesToWrite = 0; // (DWORD) strlen (buffer); DWORD dwBytesWritten = 0; If (ip == faucet = wcslen (ip) == 0) {ip = l "127.0.0.1"; } If (ssl == NULL || wcslen (ssl) == 0) {ssl = l "wrong"; } If (comp == NULL || wcslen (comp) == 0) {comp = L "true"; } Wsprintf (buffer, l "{\ n \" ip \ ": \"% ls \ ", \ n \" ssl \ ": \"% ls \ ", \ n \" compression \ ": \"% ls \ "\ N}", IP, SSL, computer applications); // swprintf (buffer, 150, l "{\ n \" ipaddress \ ": \"% ls \ ", \ n \" ssl \ ": \"% ls \ ", \ n \" compression \ ": \ "% Ls \" \ n} ", IP, SSL, computer applications); Std :: wcout & lt; & Lt; Buffer & lt; & Lt; Std :: endl; DwBytesToWrite = (wcslen (buffer)) * size (wchar_t); HFile = CreateFile (L "C: //SomeFolder//some_config.config", // GENERIC_WRITE Writing name, 0 open to write, // not share tap, // default protection CREATE_ALWAYS, // always create new File FILE_ATTRIBUTE_NORMAL, // normal file null); BErrorFlag = WriteFile (HFile, // open file handler buffer, data start to write dubbets text //, the number of bytes to write & amp; dwBytesWritten, // the number of bytes which were written); // An overlapped structure CloseHandle (hFile); Boost :: property_tree :: ptree pt; Try {boost :: property_tree :: read_json ("C: //SomeFolder//some_config.config", pt); } Hold (std :: exception & e) {std :: cout & lt; & Lt; E.what (); } {Std :: cout & lt; & Lt; Pt.get & LT; Std :: string & gt; ("IP"); } Hold (std :: exception & e) {std :: cout & lt; & Lt; E.what (); } "Content":  

File contents

  {"ip": "127.0.0.1", "SSL": "wrong", "compression": " True "}  

but fails to use read_json and returns an error:

  C: // SomeFolder // some_config.config (1): Name of the required object is such a node (ip)  

What's wrong with the code? Why can not I write the file read_json ? If I am using WriteFile wrongly, please correct me Thank you.

You want to use wptree :

  Promotion: property_tree :: wptree pt; Boost :: property_tree :: read_json ("C: //SomeFolder//some_config.config", PT); Std :: wcout & lt; & Lt; Pt.get & LT; Std :: wstring & gt; (L "IP");  

Note also L "ip" and wstring there.

Sidenote: You need const versions if you are assigning from string literals, then LPWWWWPP (LPCWRS? I think)


No comments:

Post a Comment