cstring to string

This is a discussion on cstring to string within the Windows Programming forums, part of the Platform Specific Boards category; hi all, how to convert cstring to string? thanks!...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    74

    cstring to string

    hi all,

    how to convert cstring to string?
    thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    I'd look in the manual for a suitable cstring method...
    Or you could ask Santa to help
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    You can access the inner string on CString (I'm assuming you mean the MFC/ATL version) with GetString.

    Be very careful though...std::string is often used with 1 byte chars....CString can also be unicode and GetString returns a PCXSTR

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,624
    Depends on what you do. GetString is rarely, if ever, used.
    If you need a const TCHAR*, then all you need to do is pass the object and it will automatically call CString's const TCHAR* function.
    If you need a buffer to write to, you call GetBuffer with appropriate size. This will return TCHAR*, so you can write to it. Don't forget to call ReleaseBuffer later to make sure the CString class can handle the new string you've copied into the buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Use CStringA for a GetString() that returns const char*.
    Use CStringW for a GetString() that returns const wchar*.
    Use CString for GetString() that returns const TCHAR*.

    gg

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,624
    You don't need to call GetString() to get a const TCHAR*. The operator const TCHAR* is automatically invoked when you need to pass a const TCHAR* string somewhere. Just pass the CString object itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Problems with my custom string class
    By cunnus88 in forum C++ Programming
    Replies: 15
    Last Post: 11-15-2005, 07:21 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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