在C中,字符串是字符數組(指向字符序列的指針)。在你的代碼中,相等運算符只是比較兩個完全不同的指針值。您應該使用strcmp
功能,或者使用string
類:
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <cstring> // <-- here
using namespace std;
int main(int argc, char* argv[])
{
cout << argv[1] << endl;
if (strcmp(argv[1], "-r") == 0) // <-- and here
cout << "success\n";
}
OR
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <string> // <-- here
using namespace std;
int main(int argc, char* argv[])
{
cout << argv[1] << endl;
if (string(argv[1]) == "-r") // <-- and here
cout << "success\n";
}
你不能用'=='來比較字符串,歡迎來到'C' /'C++'; D的世界歡迎來到SO以及漂亮的配置文件圖片:) – LihO