Thread: LPCTSTR ends at space

  1. #1
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27

    LPCTSTR ends at space

    I'm trying to convert STD:: basic_string<TCHAR> to LPCTSTR. It works, but the result ends at the first space in the original string.

    Code:
    basic_string<TCHAR> strOutput;
    basic_stringstream<TCHAR>  buff;
    buff << "Camera pos: ("<<camerax<<", "<<cameray<<", "<<cameraz<<")";
    buff>>strOutput;
    output=(LPCTSTR)strOutput.c_str();
    I know the conversion works because I output="Camera". If I replace all the spaces with underscores then the entire thing works. I suppose that's acceptable, but is there any way to get the spaces?

    (or a better way to convert? I still need to be able to add string versions of my variables easily, like with the stringStream above, which automatically converts my floats to strings)


    Note: the reason I'm doing this is so I can send output to the directx LPD3DXFONT->DrawText() function. Is it possible that this function just doesn't like spaces?
    Last edited by Vick jr; 05-20-2010 at 05:39 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    First, a review of TCHAR's - LPCTSTR operations/ convertion

    I don't recommend TCHAR's, but just to correct the above code:
    You have to use the TEXT("") macro for TCHAR string literals. This will put the necessary "L" in front of the literal in case TCHAR resolves to wchar_t.

    >> output=(LPCTSTR)strOutput.c_str();
    c_str() already returns the correct type - you shouldn't have to cast it.

    Consider doing to your work with either char or wchar_t strings and just call the "A" or "W" directx functions.

    gg

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by Codeplug View Post
    First, a review of TCHAR's - LPCTSTR operations/ convertion

    I don't recommend TCHAR's, but just to correct the above code:
    You have to use the TEXT("") macro for TCHAR string literals. This will put the necessary "L" in front of the literal in case TCHAR resolves to wchar_t.

    >> output=(LPCTSTR)strOutput.c_str();
    c_str() already returns the correct type - you shouldn't have to cast it.

    Consider doing to your work with either char or wchar_t strings and just call the "A" or "W" directx functions.

    gg
    He was getting confused with the Microsoft CString class where such a thing works
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No need for the LPCSTR conversions.

    Input streaming operations (like buff>>strOutput) stop at the first whitespace by default unless end of stream is reached first.

    basic_stringstream has a method named str() which returns a string object that contains a copy of the stream buffer. So try strOutput = buff.str();
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User Vick jr's Avatar
    Join Date
    May 2009
    Location
    Galifray
    Posts
    27
    Quote Originally Posted by grumpy View Post
    No need for the LPCSTR conversions.

    Input streaming operations (like buff>>strOutput) stop at the first whitespace by default unless end of stream is reached first.

    basic_stringstream has a method named str() which returns a string object that contains a copy of the stream buffer. So try strOutput = buff.str();
    Ah ha! That explains the spaces. I should have remembered that streams end with whitespace. I've had problems with that before. Thank you!

    Now, using that function and regular std::string

    Code:
    string strOutput;
    stringstream  buff;
    buff << "Camera pos: ("<<camerax<<", "<<cameray<<", "<<cameraz<<")";
    strOutput=buff.str();
    
    D3DXCreateTextureFromFileA(...,strOutput,...);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LPCTSTR operations/ convertion
    By Vick jr in forum Windows Programming
    Replies: 6
    Last Post: 06-11-2010, 10:23 AM
  2. Help in file Manipulation
    By arunvijay19 in forum C Programming
    Replies: 5
    Last Post: 02-07-2010, 05:23 AM
  3. C name space and lexical scope
    By password636 in forum C Programming
    Replies: 1
    Last Post: 09-22-2009, 09:50 AM
  4. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 02:43 PM

Tags for this Thread