#include #include using namespace std; #include "nullcodecvt.h" static inline void writeUnicodeMarkerToStream( wofstream& os ); static inline void writeUnicodeToStream( wofstream& os, const char* input ); static inline void writeUnicodeEndlToStream( wofstream& os ); static inline wstring fromUtf8toUnicode( const char* utf8 ); int main() { wofstream os; os.imbue( locale( locale::classic(), new NullCodecvt) ); os.open( "rad.csv", ios::out | ios::binary ); if( !os ) { cout << "Unable to open output file." ; exit(0); } writeUnicodeMarkerToStream( os ); // Unicode byte marker for ( int i = 0; i < 100; i++ ) { writeUnicodeToStream( os, "Forecast" ); writeUnicodeEndlToStream( os ); } return 0; } static inline void writeUnicodeMarkerToStream( wofstream& os ) { os << ( wchar_t ) 0xff; os << ( wchar_t ) 0xfe; } static inline void writeUnicodeToStream( wofstream& os, const char* input ) { wstring str = fromUtf8toUnicode( input ); os << str; if(os.fail()) cout << "Error: " << os.rdstate() << endl; } static inline void writeUnicodeEndlToStream( wofstream& os ) { os << ( wchar_t ) 0x0a; os << ( wchar_t ) 0x00; } static inline wstring fromUtf8toUnicode( const char* utf8 ) { unsigned short * result; if ( !utf8 ) return NULL; unsigned int len = strlen( utf8 ); int pos = 0; result = (unsigned short *) malloc( (len) * sizeof(unsigned short) ); unsigned short uc = 0; int need = 0; for (int i=0; i