程序告诉我,即使它们明显匹配,参数也不匹配

Program is telling me arguments don't match even when they clearly do

本文关键字:参数 不匹配 告诉我 程序      更新时间:2023-10-16

>图片 所以我的C++程序连接到我的服务器以下载并安装最新版本。当更新可用时,它将下载安装程序.exe文件,然后启动它。安装程序.exe安装新文件,然后使用"-rs"参数启动新程序。客户端/服务器端的更新代码运行良好,但由于某种原因,程序没有选择"-rs"参数。它告诉我没有指定"-rs",这很奇怪,因为如果我在 argv1 <<键入 std::cout,它会将 -rs 打印到控制台。我什至尝试制作一个快捷方式,使用"-rs"启动程序,它仍然说 -rs 不等于 -rs。

if (argc == 2)
{
std::cout << "There are 2 arguments." << std::endl;
std::cout << "'" << argv[1] << "' is the second argument." << std::endl;
if (argv[1] != "-rs")
{
std::cout << "'" << argv[1] << "' is not equal to '-rs' WTF?" << std::endl;
}
}

您正在将单个字符指针与整个字符数组进行比较,尝试给 argv != "-rs" 您的条件语句是错误的。是的,如果您采用命令行参数,那么您需要给出 2 个要比较的索引,例如 argv[index][index]

std::cout << "There are 2 arguments." << std::endl;
std::cout << "'" << argv[1] << "' is the second argument." << std::endl;
if (argv != "-rs")
{
std::cout << "'" << argv[1] << "' is not equal to '-rs' WTF?" << std::endl;
}

char argv[2][4] = {"-ws","-rs"};
std::cout << "There are 2 arguments." << std::endl;
std::cout << "'" << argv[1] << "' is the second argument." << std::endl;
if (argv[1] != "-rs")
{
std::cout << "'" << argv[1] << "' is not equal to '-rs' WTF?" << std::endl;
}