Unicode Print Trouble

I'm using windows, C++ and running a console app (multi-byte). The info is printing fine to the screen, however it is not printing to my log file correctly. Any help is appreciated. Thanks.

1
2
wprintf(L"\n   2 URL: %s\n\n", wsURL.c_str());
myfile << "\n   2 URL:\n\n" << wsURL.c_str();


wprint gives me 2 URL: cplusplus.com
myfile gives me 2 URL: 4214214214321322103302033

The full code....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
	BSTR bstr;
 	pBrowser->get_LocationURL(&bstr);
	std::wstring wsURL;
	wsURL = bstr;
 	
	size_t DSlashLoc = wsURL.find(L"://");
	if (DSlashLoc != wsURL.npos)
		{
			wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 3);
		}
	DSlashLoc = wsURL.find(L"www.");
	if (DSlashLoc == 0)
		{
		wsURL.erase(wsURL.begin(), wsURL.begin() + 4);
		}
	DSlashLoc = wsURL.find(L"/");
	if (DSlashLoc != wsURL.npos)
		{
		wsURL.erase(DSlashLoc);
		}
	wprintf(L"\n   2 URL: %s\n\n", wsURL.c_str());
	myfile << "\n   2 URL:" << wsURL.c_str();
	SysFreeString(bstr);
}
Last edited on
Is myfile of type std::wofstream? It should be.
No it's not. I'm using ofstream everywhere else, it's just this one line that I need. Tried this...

1
2
3
char LogURL;
WideCharToMultiByte( CP_ACP,WC_COMPOSITECHECK, wsURL.c_str(), -1, LogURL, 0,  NULL, NULL); 
myfile << "\n   URL:" << LogURL;


However, I'm getting an error on LogURL. (char not compatible with LPSTR)
I see this thread marked as answered, so I guess you solved the problem already.

Just FYI, std::wofstream will automatically encode to UTF-8 (which is compatible with ANSI), so you shouldn't need to worry about that conversion.

As for your use of the WideCharToMultiByte() function, it is incorrect. Refer to http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130(v=vs.85).aspx for complete details on how to call it.

In short, LogURL should be an LPSTR, not a single char, and it should point to a valid memory buffer. Normally you first do a call to WideCharToMultiByte() with a NULL buffer so the function tells you how much memory you need. Then you allocate the needed memory and call the function once more, this time with the valid buffer.
Topic archived. No new replies allowed.