I have a code, that enables proxy on Linux. It should work like this: I'm starting the program, writing the IP:port
in ("stop" to disable proxy), the program writes into terminal export http_proxy://(entered proxy)
, if I have entered "stop", it writes unset http_proxy
. Here is the code:
#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+'/');
}
}
But when I compile with g++, I get this error:
error: invalid operands of types ‘const char*’ and ‘char [20]’ to binary ‘operator+’
This is my first time with ""+a+""
. Can you help me, please?
Your error is due to the fact that you cannot use +
to concatenate string literals. Your best bet is to use std::string
to build your strings.
system(("export http_proxy=http:/"s + '/' + proxy + '/').c_str());
proxy
should also be a std::string
. That way you don't have to worry about what size to make it or buffer overflows.
Also, as Sam Varshavchik pointed out, this won't do what you want anyway. system()
creates a child process with its own environment; that goes away once the call is done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.