AskProf
November 18th, 2007, 09:30 PM
I created a new empty Win32 Application and add the source file "Test.cpp" (to easily describe the problem, I picked the problem part from my project to the test project). With the default setting, it can be successfully compiled. But if I add "UNICODE,_UNICODE", the compiling errors come out by fstream & sprintf.
The following is the code of Test.cpp".
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>
struct SPEC
{
int i;
int j;
};
TCHAR szAppName[] = TEXT("TestWin"),
szName[] = TEXT("data.dat"),
szBuffer[80];
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nShowCmd)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName,
TEXT ("The Test Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL,
hInstance, NULL) ;
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
SPEC spec;
ifstream in(szName, ios::binary | ios::nocreate);
//Problem here
switch (message)
{
case WM_CREATE:
if (!in)
{
in.read((char*)&spec, sizeof(spec));
in.close();
}
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
sprintf(szBuffer, "The width is %d and the height is %d.", spec.i, spec.j);
//Problem here
DrawText (hdc, szBuffer, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
The error message:
E:\Test\Test.cpp(68) : error C2664: '__thiscall ifstream::ifstream(const char *,int,int)' : cannot convert parameter 1 from 'unsigned short [9]' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Test\Test.cpp(87) : error C2664: 'sprintf' : cannot convert parameter 1 from 'unsigned short [80]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
And I have to remove the unicode compiling option. Doesn't Unicode Support sprintf & fstream? Anyone helps?
The following is the code of Test.cpp".
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>
struct SPEC
{
int i;
int j;
};
TCHAR szAppName[] = TEXT("TestWin"),
szName[] = TEXT("data.dat"),
szBuffer[80];
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nShowCmd)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName,
TEXT ("The Test Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL,
hInstance, NULL) ;
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
SPEC spec;
ifstream in(szName, ios::binary | ios::nocreate);
//Problem here
switch (message)
{
case WM_CREATE:
if (!in)
{
in.read((char*)&spec, sizeof(spec));
in.close();
}
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
sprintf(szBuffer, "The width is %d and the height is %d.", spec.i, spec.j);
//Problem here
DrawText (hdc, szBuffer, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
The error message:
E:\Test\Test.cpp(68) : error C2664: '__thiscall ifstream::ifstream(const char *,int,int)' : cannot convert parameter 1 from 'unsigned short [9]' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Test\Test.cpp(87) : error C2664: 'sprintf' : cannot convert parameter 1 from 'unsigned short [80]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
And I have to remove the unicode compiling option. Doesn't Unicode Support sprintf & fstream? Anyone helps?