C++ strcmp 数组
我正在使用 strcmp ,如下所示。
我正在调试值 和 ,它们是相同的,但仍然没有使该条件成立。
const char opcode_read[2] = {'0', '1'};
rc = recvfrom(s, blk_receive_full, sizeof (blk_receive_full), 0,(struct sockaddr FAR *)&sin, &fromlength);
if(rc == -1){
printf("failed: recvfrom, \n No data received \n failed code: %d\n",WSAGetLastError());
cleanup();
exit(1);
}
memcpy(blk_receive_opcode, &blk_receive_full, 2);
if (strcmp(blk_receive_opcode, opcode_data) == 0) {
}
I am using strcmp as shown below.
I am debugging the values and which are coming same but still not getting that condition true.
const char opcode_read[2] = {'0', '1'};
rc = recvfrom(s, blk_receive_full, sizeof (blk_receive_full), 0,(struct sockaddr FAR *)&sin, &fromlength);
if(rc == -1){
printf("failed: recvfrom, \n No data received \n failed code: %d\n",WSAGetLastError());
cleanup();
exit(1);
}
memcpy(blk_receive_opcode, &blk_receive_full, 2);
if (strcmp(blk_receive_opcode, opcode_data) == 0) {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,您没有比较两个字符串,因为它们不是以空终止的。您可能想使用
memcmp()
代替:From what I can tell, you aren't comparing two strings as they aren't null-terminated. You may want to use
memcmp()
instead:您可能想尝试
memcmp
。strcmp
用于空终止字符串。You might want to try
memcmp
instead.strcmp
is for null terminated strings.opcode_read 不是字符串。没有 NUL 终止。
将其大小更改为 3,这样您就可以在第三个位置找到一个 NUL。
另一种方法是使用 memcmp 而不是 strcmp,这样您就不必担心讨厌的 NUL 终止符。
recvfrom 调用也有点可怕。如果我没记错的话我的 TCPIP 的话。不保证该函数在一次调用中会返回 2 个字节。它可能在第一次调用中返回一个字节,在第二次调用中返回第二个字节。
opcode_read is not a string. There is no NUL termination.
Change its size to 3, so you pick up a NUL in the third position.
An alternative would be to use memcmp instead of strcmp so you don't have to worry about the pesky NUL terminator.
The recvfrom call is a bit scary too. If I recall my TCPIP correctly. There is no guarantee that the function will return 2 bytes in one call. It may return one byte, in the first call, and the second byte in the second call.