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

  • SEARCH
  • Home
  • 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 884217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:43:05+00:00 2026-05-15T12:43:05+00:00

all the while, I am using setw for my ANSI text file alignment. Recently,

  • 0

all the while, I am using setw for my ANSI text file alignment. Recently, I want to support UTF-8 in my text file. I found out that setw no longer work.

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

std::string wstring2string(const std::wstring& utf16_unicode) {
    //
    // Special case of NULL or empty input string
    //
    if ( (utf16_unicode.c_str() == NULL) || (*(utf16_unicode.c_str()) == L'\0') )
    {
        // Return empty string
        return "";
    }

    //
    // Consider WCHAR's count corresponding to total input string length,
    // including end-of-string (L'\0') character.
    //
    const size_t cchUTF16Max = INT_MAX - 1;
    size_t cchUTF16;
    HRESULT hr = ::StringCchLengthW( utf16_unicode.c_str(), cchUTF16Max, &cchUTF16 );

    if ( FAILED( hr ) )
    {
        throw std::exception("Error during wstring2string");
    }

    // Consider also terminating \0
    ++cchUTF16;

    //
    // WC_ERR_INVALID_CHARS flag is set to fail if invalid input character
    // is encountered.
    // This flag is supported on Windows Vista and later.
    // Don't use it on Windows XP and previous.
    //

    // CHEOK : Under Windows XP VC 2008, WINVER is 0x0600.
    // If I use dwConversionFlags = WC_ERR_INVALID_CHARS, runtime error will
    // occur with last error code (1004, Invalid flags.)
//#if (WINVER >= 0x0600)
//    DWORD dwConversionFlags = WC_ERR_INVALID_CHARS;
//#else
    DWORD dwConversionFlags = 0;
//#endif

    //
    // Get size of destination UTF-8 buffer, in CHAR's (= bytes)
    //
    int cbUTF8 = ::WideCharToMultiByte(
        CP_UTF8,                // convert to UTF-8
        dwConversionFlags,      // specify conversion behavior
        utf16_unicode.c_str(),  // source UTF-16 string
        static_cast<int>( cchUTF16 ),   // total source string length, in WCHAR's,
                                        // including end-of-string \0
        NULL,                   // unused - no conversion required in this step
        0,                      // request buffer size
        NULL, NULL              // unused
        );

    assert( cbUTF8 != 0 );

    if ( cbUTF8 == 0 )
    {
        throw std::exception("Error during wstring2string");
    }

    //
    // Allocate destination buffer for UTF-8 string
    //
    int cchUTF8 = cbUTF8; // sizeof(CHAR) = 1 byte
    CHAR * pszUTF8 = new CHAR[cchUTF8];

    //
    // Do the conversion from UTF-16 to UTF-8
    //
    int result = ::WideCharToMultiByte(
        CP_UTF8,                // convert to UTF-8
        dwConversionFlags,      // specify conversion behavior
        utf16_unicode.c_str(),  // source UTF-16 string
        static_cast<int>( cchUTF16 ),   // total source string length, in WCHAR's,
                                        // including end-of-string \0
        pszUTF8,                // destination buffer
        cbUTF8,                 // destination buffer size, in bytes
        NULL, NULL              // unused
        ); 

    assert( result != 0 );

    if ( result == 0 )
    {
        throw std::exception("Error during wstring2string");
    }

    std::string strUTF8(pszUTF8);

    delete[] pszUTF8;

    // Return resulting UTF-8 string
    return strUTF8;
}

int main() {
    // Write the file content in UTF-8
    {
        std::ofstream file;
        file.open("c:\\A-UTF8.txt");
        file << std::setw(12) << std::left << wstring2string(L"我爱你") << "????" << std::endl;
        file << std::setw(12) << std::left << "ILU" << "????";
    }

    {
        std::ofstream file;
        file.open("c:\\A-ANSI.txt");
        file << std::setw(12) << std::left << "WTF" << "????" << std::endl;
        file << std::setw(12) << std::left << "ILU" << "????";
    }
    return 0;
}

My output for A-ANSI.txt is

WTF         ????
ILU         ????

My out put for A-UTF8.txt is

我爱你   ????
ILU         ????

How can I make A-UTF8.txt’s text aligned properly?

  • 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-15T12:43:06+00:00Added an answer on May 15, 2026 at 12:43 pm

    Even in a “monospaced” font, some East Asian characters are wider than others. You also have to consider combining characters, which have no width of their own.

    There’s a wcswidth function that may do what you want.

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

Sidebar

Ask A Question

Stats

  • Questions 434k
  • Answers 434k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is a neat function in C# for reading string… May 15, 2026 at 3:10 pm
  • Editorial Team
    Editorial Team added an answer This is just a string like any other string. Once… May 15, 2026 at 3:10 pm
  • Editorial Team
    Editorial Team added an answer It's simple to do in XSLT. Add this to the… May 15, 2026 at 3:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.