Converting C++ Style strings...

This is a discussion on Converting C++ Style strings... within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use execvp which has the prototype: int execvp( const char *file, char *const argv[]); I'm using a ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Converting C++ Style strings...

    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?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    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

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    Quote Originally Posted by hk_mp5kpdw
    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.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    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

  5. #5
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    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 

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    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.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by LightsOut06
    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?
    Last edited by 7stud; 10-26-2005 at 01:58 PM.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    assume you have a vector of strings
    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;
    }
    now you have the 2d parameter.
    Last edited by Ancient Dragon; 10-26-2005 at 02:12 PM.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by 7stud
    Nope, that doesn't work.
    worked ok for me
    Code:
    std::string filename = "Myfile";
    execvp( filename.c_str(), argv);

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Ancient Dragon
    worked ok for me
    Code:
    std::string filename = "Myfile";
    execvp( filename.c_str(), argv);
    It turns out, the op wants to send the string through the 2nd argument.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating C style Strings Question
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2009, 03:19 PM
  2. Converting Integers to Strings
    By momo97 in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 12:36 AM
  3. Converting strings to char
    By Furious5k in forum C++ Programming
    Replies: 13
    Last Post: 01-02-2009, 04:26 PM
  4. Converting ifstreams to strings?
    By suzakugaiden in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2006, 09:01 PM
  5. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21