[英]Why are these two char arrays not equal?
我有这两个char数组,它们在控制台(DESKTOP-KDOD9T)上输出相同的值,一个从文件中读取,另一个通过winsock gethostname接收。 相比之下,它们并不相同。 提示您无法访问该程序。
#include "pch.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <string>
#include <fstream>
#include <WinSock2.h>
#pragma comment( lib, "Ws2_32.lib" )
int main()
{
char word[255];
std::fstream stream;
stream.open("Text.txt");
stream >> word;
std::cout << word << "\n";
WSADATA wsa_Data;
WSAStartup(0x101, &wsa_Data);
char buffer[255];
gethostname(buffer, 255);
std::cout << buffer << std::endl;
WSACleanup();
if (word == buffer)
{
std::cout << "Welcome!\n";
for (int i = 0; i < 3; ++i)
{
std::cout << ".";
Sleep(300);
}
std::cout << std::endl;
}
else
{
std::cout << "You can't access this program from that PC!\n";
Sleep(200);
exit(1);
}
getchar();
return 0;
}
数组没有可以比较的值。 它们包含多个值。
比较两个数组时,这些数组会衰减到指针,并且正在比较指针以查看它们是否指向同一对象。 当然,他们没有。
如果要比较每个字节,可以使用类似memcmp
的函数。 如果要比较两个字符串,可以使用strcmp
类的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.