使用system()函数c ++中的变量 [英] Using variables in system() function c++

查看:153
本文介绍了使用system()函数c ++中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  string line;
  ifstream myfile ("aaa.txt");
  getline (myfile,line);
  system("curl.exe -b cookie.txt -d test="+line+"  http://example.com");

它不工作!我也试过line.c_str();但它也没有工作。请帮助我。

And It doesn't work! I also tried line.c_str(); But it didnt work either. Please help me.

推荐答案

它不工作,因为你传递一个C ++字符串到C函数系统。 c_str()可以帮助,但应该将其应用于整个字符串:

It doesn't work because you're passing a C++ string to a C function system(). c_str() can help, but you should apply it to the whole string:

system(("curl.exe -b cookie.txt -d test="+line+"  http://example.com").c_str());

如下面的注释,将随机变量传递给system()可能是非常危险的,所以你应该这样做,如果你知道它可能包含什么。如果它是由用户提供或从网络接收,你可能不应该这样做。传递字符串通过某种逃逸函数或使用spawn()/ exec()/无论如何不传递给shell。

As noted in the comments below, passing random variables to system() can be quite dangerous, so you should only do that if you know exactly what it may contain. If it's supplied by the user or received from the network, you probably shouldn't do that. Pass the string through some sort of "escape" function or use spawn()/exec()/whatever else that doesn't pass it to the shell.

这篇关于使用system()函数c ++中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆