argv[i] == "-i"
In the line above you compare two pointers: char*
and const char*
, respectively.
In other words, instead of comparing argv[i]
and "-i"
two pointers are compared which are pretty much unlikely to point to the same location. As a result, the check doesn't work in your case.
You can fix it in multiple ways, for example wrap "-i"
into std::string
to make the comparison work properly:
const auto arg = std::string{ "-i" };
for(int i = 0; i < argc; i++){
if(argv[i] == arg){
test = argv[i+1];
break;
}
}
Starting with C++17 you might also use a std::string_view
:
const std::string_view sv{ "-i" };
for(int i = 0; i < argc; i++){
if(argv[i] == sv){
test = argv[i+1];
break;
}
}
which is a preferable way as it avoids a std::string
creation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…