What's wrong with strcpy(), strcat(), sprintf (), .... and C2664 error?

Stan Huang 421 Reputation points
2021-02-14T11:50:27.303+00:00

I get used to use strcat(), strcpy(), sprintf(), ... for several decades. Lately, I found they're rejected by VC++2019. From data I got from web, I found they should be accepted by VC++, but they are not. For instance, I got below error:
1>D:\programs\Neurotek\Neurotec_Biometric_11_2_SDK\Tutorials\Biometrics\CPP\FRV_Dll\FRV_Dll.cpp(366,46): error C2664: 'wchar_t *wcscpy(wchar_t *,const wchar_t *)': cannot convert argument 1 from 'char [500]' to 'wchar_t *'

for statement:
std::strcpy (temppath, REG_TEMP_TEMPL_FILE);

where
char temppath[500];

define REG_TEMP_IMAGE_FILE "C:\FR-data\FRV_service_temp\regTempImage.jpg"

How come? How can I use string processing functions like strcpy, strcat, ssprintf, as before?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,786 questions
{count} votes

Accepted answer
  1. Guido Franzke 2,186 Reputation points
    2021-02-16T07:15:26.4+00:00

    Hello,
    as already said, you use the UNICODE character set.
    strcpy, scanf, etc. are functions for non-UNICODE code.
    Have a look at the documentation of the old C-function, for example strcpy: strcpy-wcscpy-mbscpy
    I suggest you should use the TCHAR routines when you use the C-routines. Have a look in the documentation, there is _tcscpy for TCHAR.
    When you build in UNICODE, TCHAR ist the wide character, when you use MBCS, then it's the "normal" char-type.
    So when you use the TCHAR-routine, then the compiler itself uses the correct function in your character set.
    Here is an explanation for using TCHAR: What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc
    Your code would look like this:

    TCHAR temppath[500];  
    _tcscpy(temppath, REG_TEMP_TEMPL_FILE);  
    

    Regards, Guido

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.