Using variables in system() function c++

前端 未结 3 1243
慢半拍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条回答
  •  梦毁少年i
    2020-12-30 14:35

    Problem 1:

    Your problem stems from the fact that system is of signature:

    int system (const char *command);
    

    What you have is of type std::string.

    One way to fix this is to build a new std::string and then get the char pointer using c_str().

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

    Then pass the content to system.

    system(cmd.c_str());
    

    Problem 2:

    Reading data and passing it unvalidated and unclean to system will allow anyone using your program to run commands at the shell.

    This is a security risk.

提交回复
热议问题