Using variables in system() function c++

前端 未结 3 1247
慢半拍i
慢半拍i 2020-12-30 14:00


  string line;
  ifstream myfile (\"aaa.txt\");
  getline (myfile,line);
  system(\"curl.exe -b cookie.txt -d test=\"+line+\"  

        
3条回答
  •  爱一瞬间的悲伤
    2020-12-30 14:12

    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());
    

    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.

提交回复
热议问题