-3
system()
함수에 연결된 문자열 인수를 전달하는 방법은 무엇입니까? 내가 연결 문자열 인수를 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;
}
업데이트 :감사합니다. 나는 대답을 얻었다.
이 코드를 작성 했습니까? 이해 하겠니? 그렇다면이 문장에서 무엇이 행해지고 있는지'strtof (val.c_str(), NULL); 특히,'val.c_str()'? 당신은 아마 이미 그 해답을 알고 있다는 것을 이해한다면? –
문제를 컴파일하고 이해하는 데 필요한 최소 코드를 게시하십시오. 이것은 불필요한 코드를 게시하는 것보다 1000 배나 좋습니다. 예를 들면. 코드에서'getch' 구현의 필요성과'getch '호출 – user93353