Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 887571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:15:01+00:00 2026-05-15T13:15:01+00:00

Recently, I want to write a text file in unicode (UTF-16) under Windows. By

  • 0

Recently, I want to write a text file in unicode (UTF-16) under Windows.

By refering to http://www.codeproject.com/KB/stl/upgradingstlappstounicode.aspx, here is the code I am applying.

When I use Notepad to open up the document, here is the display. Newline seems disappear!!!

alt text
(source: google.com)

When I use Firefox with UTF-16 encoding selected, here is the display.

alt text
(source: google.com)

I try to open under JEdit, using the following encoding

  1. UTF-16 – Nope. Rubbish display.
  2. UTF-16BE – Nope. Rubbish display.
  3. UTF-16LE – Fine. Able to show multiple lines.

My guess is that, I need to provide additional byte ordering information? But how?

My objective is to get this UTF-16 document able to display well under Notepad, as my customer just love to use Notepad.

P/S PLEASE! Don’t ever suggest me using UTF-8. Thank you.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <locale>
#include <windows.h>
#include <tchar.h>
// For StringCchLengthW.
#include <Strsafe.h>
#include <cassert>

using namespace std;

// appearing in the NullCodecvtBase typedef.
using std::codecvt ; 
typedef codecvt < wchar_t , char , mbstate_t > NullCodecvtBase ;

class NullCodecvt
    : public NullCodecvtBase
{

public:
    typedef wchar_t _E ;
    typedef char _To ;
    typedef mbstate_t _St ;

    explicit NullCodecvt( size_t _R=0 ) : NullCodecvtBase(_R) { }

protected:
    virtual result do_in( _St& _State ,
                   const _To* _F1 , const _To* _L1 , const _To*& _Mid1 ,
                   _E* F2 , _E* _L2 , _E*& _Mid2
                   ) const
    {
        return noconv ;
    }
    virtual result do_out( _St& _State ,
                   const _E* _F1 , const _E* _L1 , const _E*& _Mid1 ,
                   _To* F2, _E* _L2 , _To*& _Mid2
                   ) const
    {
        return noconv ;
    }
    virtual result do_unshift( _St& _State , 
            _To* _F2 , _To* _L2 , _To*& _Mid2 ) const
    {
        return noconv ;
     }
    virtual int do_length( _St& _State , const _To* _F1 , 
           const _To* _L1 , size_t _N2 ) const _THROW0()
    {
        return (_N2 < (size_t)(_L1 - _F1)) ? _N2 : _L1 - _F1 ;
    }
    virtual bool do_always_noconv() const _THROW0()
    {
        return true ;
    }
    virtual int do_max_length() const _THROW0()
    {
        return 2 ;
    }
    virtual int do_encoding() const _THROW0()
    {
        return 2 ;
    }
} ;

#define IMBUE_NULL_CODECVT( outputFile ) \
{ \
(outputFile).imbue( std::locale(locale::classic(), new NullCodecvt )) ; \
} 

int main()
{
    std::wofstream file;
    IMBUE_NULL_CODECVT( file ) ;
    file.open(L"C:\\可以爱我吗.TXT", ios::out | ios::binary);
    file << L"ABC" << std::endl;
    file << L"我爱你" << std::endl;
    file << L"Bye bye" << std::endl;

    printf("done\n");
    getchar();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:15:02+00:00Added an answer on May 15, 2026 at 1:15 pm

    When you do ios::binary, the file is opened in binary mode which means newlines aren’t converted to the correct \r\n encoding on Windows.

    If you write "\r\n" instead of std::endl, it should work in Notepad. I’m not sure if this is the best solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.