string line;
ifstream myfile ("aaa.txt");
getline (myfile,line);
system("curl.exe -b cookie.txt -d test="+line+" http://example.com");
而且不起作用!我也尝试过line.c_str();但这也没有用。请帮助我。
这不起作用,因为您正在将C ++字符串传递给C函数system()。 c_str()可以帮助您,但是您应该将其应用于整个字符串:
http://example.com
如下面的注释所述,将随机变量传递给system()可能非常危险,因此只有在确切知道其可能包含的内容之后,才应该这样做。如果它是由用户提供或从网络接收的,则可能不应该这样做。通过某种“转义”函数传递字符串,或使用spawn()/ exec()/将字符串不传递给shell的任何其他函数。
问题1:
您的问题源于system(("curl.exe -b cookie.txt -d test="+line+" http://example.com").c_str());
具有签名的事实:
system
您所拥有的是int system (const char *command);
类型。
解决此问题的一种方法是构建新的std::string
,然后使用std::string
获取char指针。
c_str()
然后将内容传递到string cmd("curl.exe -b cookie.txt -d test=");
cmd += line;
cmd += " http://example.com";
。
system
问题2:
读取数据并将其未经验证和不干净的数据传递给system(cmd.c_str());
,将使任何使用您的程序的用户都能在Shell上运行命令。
这是安全隐患。
使用字符串流构建要传递给system
的字符串!
system()