gpt4 book ai didi

c++ - 如何将 system() 函数与用户输入一起使用 C++

转载 作者:行者123 更新时间:2023-11-28 06:19:01 28 4
gpt4 key购买 nike

对不起,另一个误导性的标题,很难描述它是什么。我试图让用户能够在控制台中键入内容并将其添加到 system() 命令的末尾。例如:

cout << "Input an ip in the form of xx(x).xx(x).xx(x).xx(x)" << endl;
cin >> ipstring;
system("ping");

然后在 ping 之后有 ipstring,这样用户就可以输入他们想要 ping 的内容。在 Java 中,我认为它会是这样的:

system( "ping" + ipstring )

最佳答案

system() 需要一个 char* 作为输入。

假设 ipstring 是一个 std::string,你可以这样做:

system( ("ping " + ipstring).c_str() );

如果没有,你可以使用更像这样的东西:

std::ostringstream oss;
oss << "ping " << ipstring;
system( oss.str().c_str() );

话虽这么说,你真的不应该为此使用 system()。正如其他人所说,它是一种注入(inject)攻击 vector 。如果可用,您应该使用 native API 来执行 ping,例如 IcmpSendEcho()在 Windows 上。或者第三方库。

关于c++ - 如何将 system() 函数与用户输入一起使用 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29639186/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com