如何将连接的字符串参数传递给system()函数
How to pass a concatenated string argument to the system() function?
如何将连接的字符串参数传递给system()
函数?
- 我使用
GCC 4.5.2
- 我必须使
std::string
在shell
中执行并写入其输出 - 我使用
system()
函数来执行此任务。
我不能执行这个函数,因为我只能传递char*
参数而不能传递std::string
参数。
这是我的代码。
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
void getch()
{
int c;
fflush(stdout);
while ((c = getchar()) != 'n' && c!= EOF) { /* Do Nothing */ }
}
int main()
{
string optTechniques[] = {
"-fauto-inc-dec", "-fcprop-registers", "-fdce",
"-fdefer-pop", "-fdelayed-branch", "-fdse",
........, ....., ......
}
string str;
str="gcc -Wall "+optTechniques[i]+" -o output";
cout<<"n"<<str<<"n"; /* Here it prints correctly */
system(str); /* This is generating error. */
string val;
system("sh execTime.sh");
ifstream readFile("output.log");
readFile>>val; /* Works perfectly if execution is done. */
float execTime=strtof(val.c_str(),NULL); /* Converts the string to float */
getch();
cout<<"nExecution Time: "<<execTime<<"tTechnique: "<<optTechniques[i]<<"n";
return 0;
}
更新:谢谢。
从字符串c_str
:.
system(str.c_str());
相关文章:
- C++/CLI System.AccessViolation在托管类中调用非托管函数时出现异常
- C++ 合并字符串以'system'函数错误
- 使用 system() 函数在C++程序中运行 cmd 命令
- 导出 c++ 函数并使用 c# System.AccessViolationException 中的函数
- g++ 中的 system() 函数
- System.AccessViolationException 从 C# 调用 c++ 函数
- SetWindowsHookEx 回调函数在 SYSTEM 权限上不起作用
- 如何在 C++ 中使用带有 system() 函数的字符串变量
- 通过 system() 函数调用防止野卡扩展
- 如何在 c++ 代码中的函数中调用 System() 函数
- system.security.cryptography.openssl如何调用openssl函数
- 哪一个更好?使用C 代码中的System()函数或使用源代码
- 如何在 C++ 中使用一个 System() 函数运行 2 个命令
- 什么是 stdlib system() 函数的 Win32 API 等效项?
- 需要c++ system()函数的帮助
- 获取php system()函数的完整输出
- 如何使用c++中的system()函数打开应用程序
- c++ system()函数头文件
- 在c++中使用system()函数导致命令提示符窗口出现和消失
- 如何将连接的字符串参数传递给system()函数