This is not a duplicate of something like "why simple system(variable)
doesn't work".
Solution for that one would be just store string to a variable convertable by c_str() and then just call:
system(variable.c_str())
However I am looking for a way to do that without c_str()
direct call.
So I've tried something like
class systemRunner{
private:
stringstream prepareStream;
public:
void setProgram( string s ){
prepareStream.str(""); // empty stream
prepareStream.clear(); // reset stream - !IMPORTANT!
prepareStream << "\"" << s << "\"";
}
void appendParam( string s ){ this->prepareStream << " " << s; }
void appendParam( int i ){ this->prepareStream- << " " << i; }
const char* getSystemRunCString(){
//const std::string s = ;
return this->prepareStream.str().c_str();
}
};
One would then think that this would be enaugh:
system ( systemRunner->getSystemRunCString() )
But it fails, resp. compiles fine, but when system() is called like this - system says it can't find the specified path.
However when I restore it and use c_str()
in direct system call e.g. like this:
string tmp = (string)systemRunner->getSystemRunCString();
system( tmp.c_str() );
This works fine.
One would expect that if I create a method which returns the same thing as c_str()
,
which is const char*
, that I would get the same result, but I am not getting it...
I even tried to put both inputs not in the system()
but in the file - same results so it stores the same info...
Am I missing something? Is this even possible?
PS: I am talking about using system()
in Windows 7 console application...
EDIT: Well @ravi is right about what is causing this particular example to fail - but what about the answer to the main question - in the title - is it possible to call system(variable) without directly calling c_str() in it ? :)