【发布时间】:2021-03-30 16:05:06
【问题描述】:
在 if 语句中尝试将 char 缓冲区与 std 字符串进行比较时,它没有按预期工作 这是代码
if (ConnectNamedPipe(hPipe, NULL) != FALSE) // wait for someone to connect to the pipe
{
while (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &dwRead, NULL) != FALSE)
{
/* add terminating zero */
buffer[dwRead] = 0x00;
/* do something with data in buffer */
printf("%s\n", buffer);
string cmd = bufferToString(buffer, sizeof(buffer));
printf("%s", cmd.c_str());
if (cmd.c_str() == "help") //HERE is the issue
{
printf("hello");
}
}
}
比较时不起作用 我尝试使用不同类型的 char buffer[1024] 转换为字符串,但没有得到任何地方
编辑: 到目前为止我已经尝试过
cmd.resize(dwRead);
if (cmd == string("help"))
和
if (0 == ::std::strcmp(buffer, "help"))
它们都不起作用
【问题讨论】: