Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm printing results to a log file as well as to the console. All is fine on the console screen. The log file is not good. An example of the logfile.

"Source IP:65.39.148.3400EBF5D000294844
2 URL:00407B00"

it should be
Source IP:65.39.148.34
1 URL: www.codeprojet.com
2 URL: codeproject.com

The project is in Multi-Byte
Here is the code...

C++
void PrintBrowserInfo(IWebBrowser2 *pBrowser) {
	BSTR bstr;
 	pBrowser->get_LocationURL(&bstr);
	wprintf(L"\n  1 URL: %s\n\n", bstr);
	myfile << "\n   1 URL:" << 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);
		}
	
	wprintf(L"\n   2 URL: %s\n\n", wsURL.c_str());
	myfile << "\n   2 URL:" << wsURL.c_str();
	SysFreeString(bstr);
}


I tried doing this...

myfile <<L "\n 2 URL:" << wsURL.c_str();
It did not produce the correct results.
Posted
Updated 8-Nov-11 11:12am
v2

1 solution

Sending the BSTR direct to the insertion operator is causing you this problem since it is not a recognised type. Convert it to a proper character pointer and it should work. Note that (assuming myfile is an instance of ofstream that there is no overload that accepts a wide string, as seen here[^].
 
Share this answer
 
Comments
Member 7766180 8-Nov-11 13:27pm    
Thank you Richard! Just one question.....How does one do this?
myfile is an instance of ofstream
Richard MacCutchan 8-Nov-11 16:10pm    
Also (as I should have known) you could just use the wofstream class.
Richard MacCutchan 8-Nov-11 16:11pm    
You already know how to get the wstring from the BSTR. So you need to use the WideCharToMultiByte() function to convert to ascii which you can then write to your file. Alternatively you could use an ordinary file and the fwprintf function for all writes. (posted in the wrong place before).
Member 7766180 8-Nov-11 16:27pm    
I tried this..and I'm getting a number.
wchar_t LogURL = WideCharToMultiByte( CP_UTF8, 0, wsURL.c_str(), -1,NULL, 0, NULL, NULL);
myfile << "\n URL:" << LogURL;

will try wofstream now. the problem is that I am already using ofstream everywhere else and it's just this one line.
Richard MacCutchan 8-Nov-11 16:30pm    
I guess you did not read the documentation in the link I gave you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900