Lakshmi
Lakshmi

Reputation: 179

using unicode character set in VS 2008 project settings

In a project i need to set "use unicode character set" for Configuration Properties>General>Character Set.

On compiling the project, error c2664 is returned on this code:

char Filename[25] = {0}; 
GetLocalTime(&st); 
sprintf(Filename,TEXT("C:\\CpmMicr%02d%02d%04d.log"), st.wDay,st.wMonth,st.wYear);

When I change the character set configuration to "Not set" or "Multi-byte character set" project compiles without any errors.

Please suggest me on what should be done to fix this issue.

Thanks for any potential suggestions.

Lakshmi.

Upvotes: 0

Views: 1995

Answers (2)

stijn
stijn

Reputation: 35901

extremely hard to give an exact fix without code (please post relevant part), but likely you are trying to assign a char* to a wchar* or vice-versa. For example

std::basic_string< wchar_t > someString( "test" ); //C2664 here

should be

std::basic_string< wchar_t > someString( L"test" );

edit: the problem in your code is that you use the TEXT macro so in a unicode build that will be a wide char, sprintf takes const char* though. I have no idea why it used to work in previous versions of VS? Either use wsprintf and change Filename to be a wide char, or get rid of the TEXT macro.

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941218

It is because you use the TEXT macro. That automatically puts an L before a string literal if you compile with _UNICODE defined. That blows up because you use sprintf(), a non-Unicode function. It wants a char*, not a wchar_t*.

There are three basic solutions here:

  1. Remove the TEXT macro
  2. Use TCHAR and the corresponding string functions consistently
  3. Use Unicode declarations in your code, wchar_t[] and swprintf().

No real point in 2, there is no mainstream operating system left that isn't native Unicode.

Upvotes: 1

Related Questions