string conversion to char*

This is a discussion on string conversion to char* within the C++ Programming forums, part of the General Programming Boards category; Anybody have any idea how to convert strings to char*. I am writing a program that involves using some of ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    1

    string conversion to char*

    Anybody have any idea how to convert strings to char*. I am writing a program that involves using some of the unix system calls (execv()) and I get input from the user with strings, but these functions take char* as parameters!!!

    PLEASE HELP!!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    std::string has a member function c_str() that returns a const char* rep of the string object.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is strcpy() and c_str() as the other member has mentioned.

    Kuphryn

  4. #4
    ndlarusso82
    Guest
    all my attempts of using c_str() have ended in an unhandled exception

    and the problem with the execv() call is that my input from the user is put into an array argv[] the call would be

    execv(argv[0], argv, 0);

    making the second parameter argv.c_str() gives me errors as well.

  5. #5
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Where's the string object?

    ...

    std::string a;
    a.c_str()

    The command line arguments aren't string objects
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  6. #6
    ndlarusso82
    Guest
    string buffer;
    string argv[10];

    cout << "enter agruments: ";
    getline.(cin, buffer);


    buffer gets split up into argv, for example if buffer was:
    /bin/ls -alg
    argv[0] = /bin/ls
    argv[1] = -alg

    then I want to call exec using those as parameters
    but I can't write
    execv(argv[0].c_str(), argv.c_str(), (char*)0);

    because I get errors for the argv.c_str()

  7. #7
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    execv(argv[0].c_str(), argv.c_str(), (char*)0);

    you're not indexing an element of argv. Also, what you're doing still makes no sense = | You're trying to command line stuff, right? But you're declaring argv as an array of string objects.

    ?
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's a execv() example on this page.

    The version of execv() I've seen only take two parameters, with argv one being a vector, with a null terminating element (hence the name execv). Maybe you're confusing this function with execl(). Both are documented here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 12:40 PM

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