views:

78

answers:

2

Hi,

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.

+1  A: 

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.

stijn
ok,..does the compiler not add "L" before "test" when unicode character set is selected? below is the code for which error c2664 is returned: char Filename[25] = {0}; GetLocalTime( sprintf(Filename,TEXT("C:\\CpmMicr%02d%02d%04d.log"), st.wDay,st.wMonth,st.wYear);
Lakshmi
Removing TEXT macro works as well.Since i'm migrating the old project to VS2008, there are many places where this fix has to be applied. I would like to know if any possible modifications in project settings could produce the same result?
Lakshmi
+1  A: 

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.

Hans Passant
Hi Hans Passant,solution 3 that was mentioned fixed the error. Since i'm migrating the old project to VS2008, there are many places where this fix has to be applied. Is there any other possible modification in project settings which can produce the same result?Lakshmi.
Lakshmi
Well, there's a lot of bugs in your code if it uses TEXT() or _T() in ansi string functions. The compiler will patiently tell you about them. There's otherwise no magic wand to fix bugs like this.
Hans Passant
It is a macro of a macro. IntelliSense shows you only the first level. What is the relevance of this to your question?
Hans Passant
got confused a bit.
Lakshmi