none
vc2005: strcpy(char[100], CString); doen't work. Why?

    Question

  • Hi,

    Recently I updated to Visual Studio 2005 (from 2003)

    In VC++2003 this code worked:

    char SuperString1[100];

    //m_supervar is a member variable associated with some Dialog Edit contron (CString)

    strncpy(SuperString1, m_supervar, 5);

    This does not compile in VC++2005...

    the same problem with strcpy...

     

    So question is how to convert CString to char in VC++2005?

     

    Thanks in advance

    Serhiy

    Monday, May 15, 2006 5:06 PM

Answers

  • I am sure it compiles, but you are probably talking about the warning. In VC++ 2005, the CRT string functions have been deprecated. I'd suggest that you update your code to use the new secure CRT.

    See http://msdn2.microsoft.com/en-US/library/8ef0s5kh.aspx

    Monday, May 15, 2006 6:16 PM
    Moderator
  • You should take a look at the diagnostic. My guess is that VC++2005 eventually defaults to UNICODE builds.

    If the UNICODE && _UNICODE macros are defined TCHAR maps to wchar_t not char. Hence, CString is a typedef for CStringT<wchar_t>. There is no implicit conversion from CStringT<wchar_t> to char*.

    The trivial fix is to use CStringA or _tcscpy. If you really want different encodings for the two strings there are many helpers (W2A(EX<>), std::basic_string<>, MultiByteToWideChar,mbstowcs ...).

    But you should really take a look at _UNICODE in MSDN.

    -hg

    Monday, May 15, 2006 7:13 PM

All replies

  • I am sure it compiles, but you are probably talking about the warning. In VC++ 2005, the CRT string functions have been deprecated. I'd suggest that you update your code to use the new secure CRT.

    See http://msdn2.microsoft.com/en-US/library/8ef0s5kh.aspx

    Monday, May 15, 2006 6:16 PM
    Moderator
  • Just for the sake of curiosity, why do you need to copy the text from a CString to an array of char? I'm asking because, perhaps, you can even avoid this operation... Anyway, don't you think that allocating 100 elements when you only use 5 it's a little waist? Also, should't you do the copying like this (I'm just asking)?

    strncpy(SuperString1, m_supervar, m_supervar.GetLength());

    Monday, May 15, 2006 6:28 PM
  • You should take a look at the diagnostic. My guess is that VC++2005 eventually defaults to UNICODE builds.

    If the UNICODE && _UNICODE macros are defined TCHAR maps to wchar_t not char. Hence, CString is a typedef for CStringT<wchar_t>. There is no implicit conversion from CStringT<wchar_t> to char*.

    The trivial fix is to use CStringA or _tcscpy. If you really want different encodings for the two strings there are many helpers (W2A(EX<>), std::basic_string<>, MultiByteToWideChar,mbstowcs ...).

    But you should really take a look at _UNICODE in MSDN.

    -hg

    Monday, May 15, 2006 7:13 PM
  • Thank you guys for your replys...

    Sorry, but when i try to compile

    strncpy(SuperString1, m_supervar, m_supervar.GetLength());

    it reports an error (not warning):

    error C2664: 'strncpy' : cannot convert parameter 2 from 'CString' to 'const char *'

    > perhaps, you can even avoid this operation

    How can i do that? I need to copy only few characters from one string to another...
     (not all characters)

    Thanks!

    Monday, May 15, 2006 7:15 PM
  • > My guess is that VC++2005 eventually defaults to UNICODE builds.

    You are right! i've removed the UNICODE definitions and now it compiles!!! Thank you!

    Thank you guys!

    Monday, May 15, 2006 7:46 PM
  •  wolka wrote:
    How can i do that? I need to copy only few characters from one string to another...
     (not all characters)

    I don't know, because I don't know anything about what your code is suppose to do. If you explain it, I might suggest something.

    Monday, May 15, 2006 9:19 PM
  • Or reinstate the UNICODE #defines and define

    TCHAR SuperString1[100];

     

    Tuesday, May 16, 2006 10:18 AM