- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一些代码应该使用 WriteFile
将内容写入文件。写入文件的内容类型为LPWSTR
即wchar_t *
。该文件将写入ip
、ssl
和compression
。考虑以下代码:
#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 the write
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // default security
CREATE_ALWAYS, // always create new file
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);
bErrorFlag = WriteFile(
hFile, // open file handle
buffer, // start of data to write
dwBytesToWrite, // number of bytes to write
&dwBytesWritten, // number of bytes that were 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();
}
}
文件内容会有
{
"ip": "127.0.0.1",
"ssl": "False",
"compression":"True"
}
但是使用 read_json
失败并给出错误:
C://SomeFolder//some_config.config(1): expected object name
No such node (ip)
代码有什么问题?为什么 read_json
不能读取写入的文件?如果我错误地使用了 WriteFile
,请纠正我。谢谢。
最佳答案
你想使用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");
还要注意那里的 L"ip"
和 wstring
。
旁注:如果从字符串字面量赋值,则需要 LPWSTR 指针(LPCWSTR?我猜)的 const 版本
关于c++ - Boost Property Tree Json 读取包含 LPWSTR 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29091835/
我在将值转换为 LPWSTR 时遇到困难。我正在获取一个注册表值,并尝试将结果作为 LPWSTR 返回。看起来使用 RegQueryValueExW 的注册表调用适用于各种类型来存储结果,但我无法将它
我正在按照本教程在 Windows 中进行串行通信 (http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/
编译C++项目时出现以下错误。 Error C2664 'BOOL CryptBinaryToStringW(const BYTE *,DWORD,DWORD,LPWSTR,DWORD *)': ca
我有一个函数,它只接受一个 LPWSTR 作为参数。但是,我需要在它的末尾添加整数。 这是一个示例伪代码: int theNumber = 5; LPWSTR myLPWSTR = L"Number
好的,我的程序有问题。我之前提出过一个问题,但没有人真正理解我的问题,所以这次我尝试了一种新的方法。 如果你好奇这是我的问题: My program takes arguments in the fo
这个问题在这里已经有了答案: How to convert std::string to LPCWSTR in C++ (Unicode) (6 个答案) 关闭 8 年前。 我很难将字符串转换为 L
我正在尝试初始化 MENUITEMINFO 以调用 InsertMenuItem。尝试将 const 字符串分配给 dwTypeData 时,出现错误。以下代码来自 MSDN 示例。 两种类型的赋值都
我很少直接使用Windows API,但我遇到了一种不可避免的情况,我不知道如何处理它。 MS API 文档引用了 API 调用的参数,例如 out Something LPWSTR 所以我显然必须提
我正在尝试编码一个如下所示的 C++ 结构: typedef struct _SOME_STRUCT { DWORD count; LPWSTR *items; } “items”
我正在替换一个旧的 DLL,它有一个函数,通过引用传递一个 LPWSTR。这些函数应该创建一个新的 wchar 缓冲区并返回新创建的 LPWSTR 指针。我找到了解决方案,但我认为它可以更好地解决!?
我正在使用 WinAPI GetLogicalDriveStrings() 需要 LPWSTR 的函数并且想知道是否有更安全的方法来确保没有内存泄漏。 目前我构造了一个指向缓冲区的初始指针 buf使用
我正在尝试使用 cmd.exe 执行一些操作,但我想隐藏 cmd.exe。当我尝试使用完整路径而不是 cmd.exe 时,我总是会收到此错误: char Process[] = "C:\\WINDOW
我有一个 LPWSTR 类型的变量,其中包含 "true" 或 "false"。有没有一种方法可以将其转换为具有 true 或 false 的 bool 类型的变量? 理想情况下我想做这样的事情: F
我已经使用 wstringstream 构建了一个字符串,需要将其分配给类型为 LPWSTR 的 struct 成员。我尝试使用 my_stringstream.str().c_str() 但出现以下
根据我的理解(如果我错了请纠正我),它们之间的唯一区别是字符串是否可以被调用的函数修改。 ( PCWSTR , LPWSTR ) 我现在正尝试将字符串从 C# 传递到需要 PCWSTR 的函数,但我能
在 Visual C++ 中,我有一个 LPWSTR mystring; 它已经在代码的其他地方定义了。 我想创建一个新的 LPWSTR 包含: "hello " + mystring + " bla
作为一个 C++ 新手,我似乎无法正确地做到这一点,尽管我知道它应该很简单:{ - 我需要创建一个 LPWSTR 数组,然后用唯一的字符串填充它。我们的想法是做这样简单的事情: LPWSTR *wsz
这可能是一个非常愚蠢的问题,但这是我一直在努力解决的问题。在方法中更改 LPWSTR 后,它似乎只针对该特定方法进行更改,并在之后立即恢复。我知道全局变量是邪恶的,但这不是我的选择,因为它需要更改相当
Function CommandLineToArgvW 给了我 LPWSTR 类型的命令行参数。我需要 string 中的这些参数。有人能告诉我如何将 LPWSTR 转换为 string 吗? 我正在
我有一个 LPTSTR 类型的变量,我将其打印到 std::cout与 << 。在 ANSI 系统中(不知道它是在哪里确定的)它工作得很好,它打印了字符串。现在,在 Unicode 系统中,我得到的是
我是一名优秀的程序员,十分优秀!