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 842673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:00:02+00:00 2026-05-15T06:00:02+00:00

What is the correct way to read Unicode files line by line in C++?

  • 0

What is the correct way to read Unicode files line by line in C++?

I am trying to read a file saved as Unicode (LE) by Windows Notepad.

Suppose the file contains simply the characters A and B on separate lines.

In reading the file byte by byte,
I see the following byte sequence (hex) :

FE FF 41 00 0D 00 0A 00 42 00 0D 00 0A 00

So 2 byte BOM, 2 byte ‘A’, 2byte CR , 2byte LF, 2 byte ‘B’, 2 byte CR, 2 byte LF .

I tried reading the text file using the following code:

   std::wifstream file("test.txt");
   file.seekg(2); // skip BOM
   std::wstring A_line;
   std::wstring B_line;
   getline(file,A_line);  // I get "A"
   getline(file,B_line);  // I get "\0B"

I get the same results using >> operator instead of getline

   file >> A_line;
   file >> B_line;

It appears that the single byte CR character is
is being consumed only as the single byte.
or CR NULL LF is being consumed but not the high byte NULL.
I would expect wifstream in text mode would read the 2byte CR and 2byte LF.

What am I doing wrong?
It does not seem right that one should have to read a text file
byte by byte in binary mode just to parse the new lines.

  • 1 1 Answer
  • 1 View
  • 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-15T06:00:02+00:00Added an answer on May 15, 2026 at 6:00 am

    std::wifstream exposes the wide character set to your program, which is typically UCS-2 on Windows and UTF-32 on Unix, but assumes that the input file is still using narrow characters. If you want it to behave using wide characters on disk, you need to use a std::codecvt<wchar_t, wchar_t> facet.

    You should just be able to find your compiler’s implementation of std::codecvt<char, char> which is also a non-converting code conversion facet, and change the chars to wchar_ts.

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

Sidebar

Related Questions

Using Python, what's the correct way to set a file to be read-only when
What would be the correct way to create a fully URL-encoded file:// URI from
What is the correct way to format the document.formToSubmit.submit() line? var formToSubmit = 'postcomment'
What is the correct way to do this? MySqlDataReader reader = command.ExecuteReader(); while (reader.Read())
Is there a correct way to read the arguments to a python application? Example:
On a ChipworkX device we would read files using: File.ReadAllBytes(filename); But if we try
What is the most correct way to perform MySQL Queries using PHP? I read
I'm trying to work out the correct way to handle populating an array with
I was wondering what the correct way of issuing an asynchronous read operation on
I'm confused with what is the correct way to break lines. I read somewhere

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.