Having a string within a system()

前端 未结 2 854
孤城傲影
孤城傲影 2021-01-29 09:56

How can I use a string within a system(). ex: (input is the string)

system(\"open -a Google Chrome\" \"http://www.dictionary.reference.com/browse/\" + input + \"         


        
相关标签:
2条回答
  • 2021-01-29 10:25

    system is available in cstdlib header.

    Function takes a c-style string as a parameter. + doesn't append string literals.

    So try -

    std::string cmd("open -a Google Chrome");
    cmd += " http://www.dictionary.reference.com/browse/" + input + "?s=t";
    
    // In the above case, operator + overloaded in `std::string` is called and 
    // does the necessary concatenation.
    
    system(cmd.c_str());
    
    0 讨论(0)
  • 2021-01-29 10:28

    Have you included the stdlib header?

    No matching function for call to 'system' typically occurs when it cannot resolve a function with that signature.

    EG:

    #include <stdlib.h> // Needed for system().
    
    int main()
    {
        system("some argument"); 
        return 1;
    }
    

    And don't forget to .c_str() your std::string variable when passing it in as an argument.

    See:

    1. system() documentation.
    2. This SO answer.
    0 讨论(0)
提交回复
热议问题