Thread: CString to char - possible??

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    Question CString to char - possible??

    I have a class which inlcudes this line for the function -

    inline CString GetFile() {return File;}

    I am trying to get data in from a dialog list box, using the OnDblClick() this is the code as it stands.
    Essentially I have a template filename

    (c:\dave......setup.exe)

    and the object selected has a method which gets the filename. I want it in the middle of the startpath string. (startpath will be startpath+filename+program)


    char startpath[80]("c:\\dave\\");
    char program[90]("setup.exe");
    char path[100];
    CDataObject *pObject;
    pObject=0;
    path=(pObject->GetFile());
    strcat(startpath, program);

    spawnl(P_WAIT, startpath, startpath, NULL);

    I can zip the whole thing to you if this doesn't make any sense, but i need to get the File ( which is a string, and append it to the startpath variable.

    Cheers

    dave

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    You need a null terminated version of the CString object. I think you can get it with the CString:perator LPCTSTR, but as you know, I don't use MFC.

    From the help...

    >>>
    CString:perator LPCTSTR
    Use this casting operator to access the null-terminated C string contained in a CString object. No characters are copied; only a pointer is returned. Be careful with this operator. If you change a CString object after you have obtained the character pointer, you may cause a reallocation of memory that invalidates the pointer.

    Syntax
    operator LPCTSTR ( ) const;
    <<<

    ...edit...

    that should be a ":" followed by a "o".. Damn smilies crap!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    adrianxw is completely right, you can use operator LPCTSTR.
    In fact, it will be used automatically for you if you give a CString
    object where a const char* is needed.

    Alternatively you can use the Format function to do this:

    CString strPath;
    CString strMiddlePart;

    strMiddlePart = GetYourStringFromSomeWhere();

    // Format works like sprintf
    strPath.Format( "C:\\Dave\\%s\\setup.exe", strMiddlePart );

    // conversion to const char* [= LPCTSTR] called automatically
    spawnl(P_WAIT, strPath, strPath, NULL);
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    adrian, you can turn off the smilies. See:
    ... CString::operator LPCTSTR ...
    Also, if you use the "code" tage you shouldn't have that problem, and you can even keep the smilies.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> you can turn off the smilies

    I was aware I could turn them off for me, but was not sure that turning them off for me would affect my posts when viewed by others, (i.e., when I preview I see ":" "o", but someone with smilies sees ).

    I am also aware of the code tags, which I use when posting code sections, however, the code tag can make phat messages which annoy everyone, so for one or two lines, I tend not to bother.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM