0

我正在尝试使用 signal11 的 hidapi(此处)写入隐藏设备。在我的故障排除中,我注意到字符串的一部分没有显示在控制台上。这是我的代码示例

//device is a hid device and is assigned to in another part of the program.
//dataBuffer is a struct with only a char array called "buffer" and an int which is the size of the array called "size"
void DeviceCommands::Write(hid_device* device, dataBuffer* buf)
{
    std::cout << "Attempting write >> buffer...\n";
    buf->buffer[0] = 0;
    std::cout << "Written to buffer...\n" << "writing buffer to device\n";
    int res = hid_write(device, buf->buffer, sizeof(buf->buffer));
    std::cout << "Write success: "  + '\n';
    std::cout << "Write complete\n";
}

我期待控制台返回以下内容:

Attempting write >> buffer...
Written to buffer...
writing buffer to device
Write success: (0 if the write succeeds, -1 if it fails)
Write complete

但相反,会发生这种情况:

Attempting write >> buffer...
Written to buffer...
writing buffer to device
ess: Write complete

“写成功”、结果和换行符都不见了,我对 c++ 有点陌生,但我有 c# 的经验。我只是感到困惑,非常感谢您的帮助,提前感谢并询问您是否需要更多信息!

4

2 回答 2

0

这一行:

std::cout << "Write success: "  + '\n';

将打印"Write success: "偏移量为 10 个字符的字符串,即\n. 因此,您ess在屏幕上看到。

你可能想要:

std::cout << "Write success: " << res << "\n";

假设res退货0-1根据需要。

于 2020-07-06T23:16:08.533 回答
0

不要将字符“添加”到字符串中。它不会做你所期望的。

在这里,您认为您正在将换行符添加到字符串“写入成功”中,而实际上您是在告诉编译器获取常量字符串并且仅从第 10 个字符开始流式传输。请记住,这里的常量字符串只是一个字符数组,单个字符 '\n' 被转换为数字 10。

您也错过了流式传输的结果。

所以你的倒数第二行应该是:

std::cout << "Write success: " << res <<  std::endl;
于 2020-07-06T23:24:50.320 回答