Some problen in converting char * to LPCWSTR
-
Hi I am facing some problem in the following code .Actually I am not able to print the full data present in my LPCWSTR .Here is the code
....
SYSTEMTIME current_time;.GetLocalTime(¤t_time);
char *str =new char[2000];
sprintf(str, "%4d/%2d/%2d %2d:%2d:%2d:%2d\n", current_time.wYear,current_time.wMonth,current_time.wDay,current_time.wHour,current_time.wMinute,current_time.wSecond,current_time.wMilliseconds);
int i;
wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));
i = mbstowcs( pwc, str, sizeof(str) );
WriteToLogFile(pwc);
delete [] str;
In the log file I just want to print the year date and time but only the year gets printed. Here is the log file
void WriteToLogFile(LPCWSTR strData)
{
try
{
ofstream myfile;
myfile.open("\\MyData\\Log.txt",ios::app);int iSize = WideCharToMultiByte(CP\_ACP,0,strData, -1, NULL, 0, NULL, NULL ); char\* lpNarrow = new char\[iSize\]; lpNarrow\[0\] = 0; iSize= WideCharToMultiByte(CP\_ACP, 0,strData, -1, lpNarrow, iSize, NULL, NULL ); myfile<
Any help on this..
Thanks in advance
-
Hi I am facing some problem in the following code .Actually I am not able to print the full data present in my LPCWSTR .Here is the code
....
SYSTEMTIME current_time;.GetLocalTime(¤t_time);
char *str =new char[2000];
sprintf(str, "%4d/%2d/%2d %2d:%2d:%2d:%2d\n", current_time.wYear,current_time.wMonth,current_time.wDay,current_time.wHour,current_time.wMinute,current_time.wSecond,current_time.wMilliseconds);
int i;
wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));
i = mbstowcs( pwc, str, sizeof(str) );
WriteToLogFile(pwc);
delete [] str;
In the log file I just want to print the year date and time but only the year gets printed. Here is the log file
void WriteToLogFile(LPCWSTR strData)
{
try
{
ofstream myfile;
myfile.open("\\MyData\\Log.txt",ios::app);int iSize = WideCharToMultiByte(CP\_ACP,0,strData, -1, NULL, 0, NULL, NULL ); char\* lpNarrow = new char\[iSize\]; lpNarrow\[0\] = 0; iSize= WideCharToMultiByte(CP\_ACP, 0,strData, -1, lpNarrow, iSize, NULL, NULL ); myfile<
Any help on this..
Thanks in advance
-
Hi I am facing some problem in the following code .Actually I am not able to print the full data present in my LPCWSTR .Here is the code
....
SYSTEMTIME current_time;.GetLocalTime(¤t_time);
char *str =new char[2000];
sprintf(str, "%4d/%2d/%2d %2d:%2d:%2d:%2d\n", current_time.wYear,current_time.wMonth,current_time.wDay,current_time.wHour,current_time.wMinute,current_time.wSecond,current_time.wMilliseconds);
int i;
wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));
i = mbstowcs( pwc, str, sizeof(str) );
WriteToLogFile(pwc);
delete [] str;
In the log file I just want to print the year date and time but only the year gets printed. Here is the log file
void WriteToLogFile(LPCWSTR strData)
{
try
{
ofstream myfile;
myfile.open("\\MyData\\Log.txt",ios::app);int iSize = WideCharToMultiByte(CP\_ACP,0,strData, -1, NULL, 0, NULL, NULL ); char\* lpNarrow = new char\[iSize\]; lpNarrow\[0\] = 0; iSize= WideCharToMultiByte(CP\_ACP, 0,strData, -1, lpNarrow, iSize, NULL, NULL ); myfile<
Any help on this..
Thanks in advance
In addition to my first post this
anuhoho wrote:
i = mbstowcs( pwc, str, sizeof(str) );
copies only 4(32bit)/8(64bit) chars to your 2 wchar_t long string, because the call
sizeof(str)
returns the length of an char* and not the length of the string.Greetings Covean
-
anuhoho wrote:
wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));
Maybe you should allocate more than 2 bytes for your string. ;P
Greetings Covean
-
In addition to my first post this
anuhoho wrote:
i = mbstowcs( pwc, str, sizeof(str) );
copies only 4(32bit)/8(64bit) chars to your 2 wchar_t long string, because the call
sizeof(str)
returns the length of an char* and not the length of the string.Greetings Covean
-
Something like this:
int nStrLen = strlen(str);
wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t) * (nStrLen + 1));
i = mbstowcs( pwc, str, nStrLen );Hope this work, couldn't test it.
Greetings Covean