How to write UTF-8 widestring to text VC++

find the reason why using wstring instead of string???

ofstream simply wont write wstring, forcing wofstream without codecvt will triger an SEHException (unmanaged code exeption).

wrapp your code with try catch with SEHEException pointer handler

try{
//some fout
}catch ( System::Runtime::InteropServices::SEHException ^ex )
{}

and then

Include this code:

#include <codecvt>

call your function

std::wofstream fout;
fout.open(fileOutput);
fout.imbue(std::locale(fout.getloc(),new std::codecvt_utf8_utf16<wchar_t>));

fout<<add_wstring_here<<"\n";

fout.close();

it works with vs 2010, dll export to vc# 2013 works very well too 🙂

courtesy to this page

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s