C++ 字符串中的字符被忽略
the characters in a c++ string are being ignored
我正在尝试使用 signal11 的 hidapi (here) 写入隐藏设备。
在我的故障排除中,我注意到字符串的一部分没有显示到控制台。
这是我的代码示例
//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";
}
我希望控制台 return 以下内容:
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
缺少“Write succ”、结果和换行符,我对 c++ 有点陌生,但我有使用 c# 的经验。我只是很困惑,非常感谢您的帮助,提前致谢并询问您是否需要更多信息!
这一行:
std::cout << "Write success: " + '\n';
会打印字符串"Write success: "
,偏移10个字符,即\n
的ascii值。因此你在屏幕上看到 ess
。
你可能想要:
std::cout << "Write success: " << res << "\n";
假设 res
returns 0
或 -1
根据需要。
不要'add'一个字符到一个字符串。它不会像你期望的那样。
在这里您认为您正在将换行符添加到您的字符串“Write success”,而实际上您是在告诉编译器获取您的常量字符串并且只从第 10 个字符开始流。请记住这里的常量字符串只是一个字符数组,单个字符 '\n' 被转换为数字 10.
您还错过了流式传输的结果。
所以倒数第二行应该是:
std::cout << "Write success: " << res << std::endl;
我正在尝试使用 signal11 的 hidapi (here) 写入隐藏设备。 在我的故障排除中,我注意到字符串的一部分没有显示到控制台。 这是我的代码示例
//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";
}
我希望控制台 return 以下内容:
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
缺少“Write succ”、结果和换行符,我对 c++ 有点陌生,但我有使用 c# 的经验。我只是很困惑,非常感谢您的帮助,提前致谢并询问您是否需要更多信息!
这一行:
std::cout << "Write success: " + '\n';
会打印字符串"Write success: "
,偏移10个字符,即\n
的ascii值。因此你在屏幕上看到 ess
。
你可能想要:
std::cout << "Write success: " << res << "\n";
假设 res
returns 0
或 -1
根据需要。
不要'add'一个字符到一个字符串。它不会像你期望的那样。
在这里您认为您正在将换行符添加到您的字符串“Write success”,而实际上您是在告诉编译器获取您的常量字符串并且只从第 10 个字符开始流。请记住这里的常量字符串只是一个字符数组,单个字符 '\n' 被转换为数字 10.
您还错过了流式传输的结果。
所以倒数第二行应该是:
std::cout << "Write success: " << res << std::endl;