如何连接字符串并传递给system()调用? [英] How to concatenate string and pass to a system() call?

查看:61
本文介绍了如何连接字符串并传递给system()调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里尝试使用一些代码,以允许用户输入IP地址并对其进行ping操作.我现在正在通过system()函数进行操作.我的问题是我无法让system()函数读取字符串以及变量.在这里看看:

I have a bit of code here that I'm trying to use to allow the user to put in an IP address and have the code ping it. I'm just doing through the system() function right now. My problem is that I can't have the system() function read a string as well as a variable. Have a look here:

system("ping " + INPUT);

INPUT之前已经定义为字符串,并且包含用户放入程序中以ping的IP地址.如果我使用+运算符,Visual Studio会给我一个错误,因为我在两个字符串上使用了数学运算符.我使用什么运算符来组合两个不同字符串的内容?

INPUT is already defined earlier, as a string, and it holds the IP address that the user puts into the program to ping. If I use the + operator, Visual Studio gives me an error because I'm using a mathematical operator on two strings. What operator do I use to combine the contents of two different strings?

推荐答案

问题不在于字符串串联.这是因为 system 需要一个指向 char 的指针,而不是 std :: string 的指针.所以你需要像

The problem is not with the string concatenation. It is that system requires a pointer to char, not an std:: string. So you need something like

std::string s2 = "ping" + INPUT;
system(s2.c_str());

这篇关于如何连接字符串并传递给system()调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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