I'm trying to use execvp which has the prototype:
int execvp( const char *file, char *const argv[]);
I'm using a C++ style string to get input from the user. How can I convert this C++ style string into something that execvp can use for arguments?
I'm trying to use execvp which has the prototype:
int execvp( const char *file, char *const argv[]);
I'm using a C++ style string to get input from the user. How can I convert this C++ style string into something that execvp can use for arguments?
Call the string's c_str() member function which returns a const char* to the data stored in the string.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
i tried that that's still not what execvp wants for the second arg.Originally Posted by hk_mp5kpdw
The second argument should be an array of character strings and not a single character string. How exactly are you attempting to call this function? Show some code.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
search google man, i found it in less than 5 seconds...http://www.opengroup.org/onlinepubs/.../xsh/exec.html
-"What we wish, we readily believe, and what we ourselves think, we imagine others think also."PHP Code:
sadf
i know what execvp accepts. My problem is that I have a C++ style string not const char *, or char *const argv[]. I wouldnt to know how to convert the string into something execvp can accept. There is no code, this is just a basic question.
The function is expecting an array of pointers. You have one pointer(from c_str() ). So, how about creating an array of pointers, putting your one pointer into the array, and then passing the array to the function?Originally Posted by LightsOut06
Last edited by 7stud; 10-26-2005 at 01:58 PM.
assume you have a vector of strings
now you have the 2d parameter.Code:int main() { vector<string> array; int i; char **argv = new char*[array.size()+1]; vector<string>::iterator it; for(i = 0, it = array.begin(); it != array.end(); ++it, ++i) { argv[i] = const_cast<char*>((*it).c_str()); } argv[i] = 0; return 0; }
Last edited by Ancient Dragon; 10-26-2005 at 02:12 PM.
Nope, that doesn't work.
The problem is that c_str() returns a pointer to a const(which means you can't change what the pointer points to), and the function requires an array of const pointers. Requiring a const pointer means the function wants to be able to change the value the pointer points to(but the pointer cannot be made to point elsewhere). So, it sounds like you have to do some casting.
Last edited by 7stud; 10-26-2005 at 03:25 PM.
worked ok for meOriginally Posted by 7stud
Code:std::string filename = "Myfile"; execvp( filename.c_str(), argv);
It turns out, the op wants to send the string through the 2nd argument.Originally Posted by Ancient Dragon
Here is an example that avoids casting:
Code:#include <iostream> #include <string> using namespace std; int execvp(char *const argv[]) { cout<<argv[0]<<endl; return 0; } int main() { string str = "See Spot run."; int len = str.length(); //create the correct type of pointer and point it to an array of chars: char* const myArr = new char[len + 1]; //copy string into the array: for(int i=0; i < len; i++) { myArr[i] = str[i]; } myArr[len]='\0'; //create the array of pointers required by the function: char* const argv[] = {myArr}; execvp(argv); //clean up: delete [] myArr; return 0; }
Last edited by 7stud; 10-26-2005 at 08:32 PM.