将字符串数组传递给系统函数
Passing a string array to system function?
我有一个包含 3 个不同命令行命令的字符串数组。我没有写出 3 个系统函数,而是在尝试学习如何在 for 循环中将这些命令传递给系统函数(或类似的函数,即 exec())。我在尝试弄清楚如何将此字符串数组一次传递到系统函数中时遇到麻烦。目标是获取每个的退出状态,并在返回错误时中断 for 循环。
std::string arrString[3] = {"something","another thing","a final thing"}
int i;
for(i=0; i<3; i++)
{
if (system(/*Something*/))
;//Do something...
}
编辑:这输出发生了错误,但不应该。
std::string arrString[4] = {"cmd","cmd","cmd"};
int i;
for(i=0; i<3; i++)
{
if (system(arrString[i].c_str())==0) {
OutputDebugStringW(L"It works!");
}
else
{
OutputDebugStringW(L"It doesnt work :(");
}
}
system
需要char*
,所以你需要对数组的每个元素调用c_str
:
std::string arrString[3] = {"something","another thing","a final thing"}
int i;
for(i=0; i<3; i++) {
if (system(arrString[i].c_str())) {
//Do something...
}
}
system(arrString[i])
然后检查退出代码并在适当时中断循环。
您必须先使用 c_str()
函数将std:string
转换为char*
:
system(arrString[i].c_str())
相关文章:
- 为什么系统函数总是在C++中返回已转移的退出状态?
- 如何检查函数是否在LLVM Instrumentation pass的ModulePass的系统头文件中定义?
- 为什么系统日志有两个不同的函数声明?
- 调用不可用的函数"系统":在 iOS 上不可用
- "<某些系统标头>:错误:"<在此处插入函数>"在移动源文件后不是"std"的成员
- C++系统函数不接受字符串?
- 从C 中的系统函数中获取整数(大于255)
- 解决系统函数以运行来自 vc++ 的 arg 的 bat 文件
- 我需要以相反的方式复制一个长整型数组,我需要系统函数,例如 memcpy 有没有
- 将字符串数组传递给系统函数
- 如何在 Linux 上使用 C++ 中的系统函数
- 如何在linux中使用c或c++在不使用system()或exec()函数的情况下调用像pwd或ls-l这样的系统函数
- 如何创建与系统函数同名的方法
- 如何使用C++应用程序和系统函数来执行java控制台应用程序
- Windows上的直接文件系统函数调用
- GetOpenFileName和系统函数调用运行时错误c++ win32 api
- VS2012,使用系统函数调用exe,这会导致崩溃
- 从C调用系统c++函数
- Turbo C++系统函数运行一个可执行文件
- 在系统函数中输入字符串