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?
Printable View
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.
i tried that that's still not what execvp wants for the second arg.Quote:
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.
search google man, i found it in less than 5 seconds...http://www.opengroup.org/onlinepubs/.../xsh/exec.html
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?Quote:
Originally Posted by LightsOut06
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;
}
Nope, that doesn't work. :eek:
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.
worked ok for meQuote:
Originally 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.Quote:
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;
}