[英]Proxy script on c++ for linux
我有一個代碼,可以在 Linux 上啟用代理。 它應該是這樣工作的:我正在啟動程序,將IP:port
寫入(“stop”以禁用export http_proxy://(entered proxy)
,如果我輸入了“stop”,程序將寫入終端export http_proxy://(entered proxy)
,它寫入unset http_proxy
。 這是代碼:
#include <iostream>
using namespace std;
int main(){
char proxy[20];
char choice;
cout << "Enter ip:port ('stop' to disable): ";
cin >> proxy;
if(proxy != "stop"){
cout << "Do you want to use it as ftp(y/n): "
cin >> chioce;
}
if(proxy == "stop"){
system("unset http_proxy");
system("unset ftp_proxy");
}
else if(choice == 'y'){
system("export http_proxy=http:/"+'/'+proxy+'/');
system("export ftp_proxy=http:/"+'/'+proxy+'/');
}
else{
system("export http_proxy=http:/"+'/'+proxy+'/');
}
}
但是當我用 g++ 編譯時,我得到這個錯誤:
error: invalid operands of types ‘const char*’ and ‘char [20]’ to binary ‘operator+’
這是我第一次使用""+a+""
。 你能幫我嗎?
您的錯誤是由於您不能使用+
來連接字符串文字。 最好的辦法是使用std::string
來構建字符串。
system(("export http_proxy=http:/"s + '/' + proxy + '/').c_str());
proxy
也應該是std::string
。 這樣你就不必擔心它的大小或緩沖區溢出。
此外,正如 Sam Varshavchik 指出的那樣,這無論如何都不會做你想做的。 system()
創建一個具有自己環境的子進程; 一旦呼叫完成,它就會消失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.