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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:36:07+00:00 2026-05-25T19:36:07+00:00

I have to first read a file in Cyrillic, then randomly pick random number

  • 0

I have to first read a file in Cyrillic, then randomly pick random number of lines and write modified text to a different file. No problem with Latin letter, but I run into a problem with Cyrillic text, because I get some rubbish. So this is how I tried to do the thing.

Say, file input.txt is

ааааааа
ббббббб
ввввввв

I have to read it, and put every line into a vector:

vector<wstring> inputVector;
wstring inputString, result;
wifstream inputStream;
inputStream.open("input.txt");
while(!inputStream.eof())
{
    getline(inputStream, inputString);              
    inputVector.push_back(inputString);
}
inputStream.close();    

srand(time(NULL));
int numLines = rand() % inputVector.size();
for(int i = 0; i < numLines; i++)
{
    int randomLine = rand() % inputVector.size();
    result += inputVector[randomLine];
}

wofstream resultStream;
resultStream.open("result.txt");
resultStream << result;
resultStream.close();

So how can I do work with Cyrillic so it produces readable things, not just symbols?

  • 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-25T19:36:08+00:00Added an answer on May 25, 2026 at 7:36 pm

    Because you saw something like ■a a a a a a a 1♦1♦1♦1♦1♦1♦1♦ 2♦2♦2♦2♦2♦2♦2♦ printed to the console, it appears that input.txt is encoded in a UTF-16 encoding, probably UTF-16 LE + BOM. You can use your original code if you change the encoding of the file to UTF-8.

    The reason for using UTF-8 is that, regardless of the char type of the file stream, basic_fstream‘s underlying basic_filebuf uses a codecvt object to convert a stream of char objects to/from a stream of objects of the char type; i.e. when reading, the char stream that is read from the file is converted to a wchar_t stream, but when writing, a wchar_t stream is converted to a char stream that is then written to the file. In the case of std::wifstream, the codecvt object is an instance of the standard std::codecvt<wchar_t, char, mbstate_t>, which generally converts UTF-8 to UCS-16.

    As explained on the MSDN documentation page for basic_filebuf:

    Objects of type basic_filebuf are created with an internal buffer of type char * regardless of the char_type specified by the type parameter Elem. This means that a Unicode string (containing wchar_t characters) will be converted to an ANSI string (containing char characters) before it is written to the internal buffer.

    Similarly, when reading a Unicode string (containing wchar_t characters), the basic_filebuf converts the ANSI string read from the file to the wchar_t string returned to getline and other read operations.

    If you change the encoding of input.txt to UTF-8, your original program should work correctly.

    For reference, this works for me:

    #include <cstdlib>
    #include <ctime>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main()
    {
        using namespace std;
    
        vector<wstring> inputVector;
        wstring inputString, result;
        wifstream inputStream;
        inputStream.open("input.txt");
        while(!inputStream.eof())
        {
            getline(inputStream, inputString);
            inputVector.push_back(inputString);
        }
        inputStream.close();
    
        srand(time(NULL));
        int numLines = rand() % inputVector.size();
        for(int i = 0; i < numLines; i++)
        {
            int randomLine = rand() % inputVector.size();
            result += inputVector[randomLine];
        }
    
        wofstream resultStream;
        resultStream.open("result.txt");
        resultStream << result;
        resultStream.close();
    
        return EXIT_SUCCESS;
    }
    

    Note that the encoding of result.txt will also be UTF-8 (generally).

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

Sidebar

Related Questions

I have to read a binary file in python. This is first written by
I have read my first book on c# and feel completely clueless about LINQ.
First let me say I have read this useful article thoroughly and am using
I'm reading lines from a file, and I believe that once I've read all
I have a text file named abc.txt . The file have info like this
I have a text file in my computer which I am reading form my
I have a read file method that reads the date from the start of
I am using iostream to collect data from a device.First I Write file in
I have a file like name1=value1 name2=value2 I need to read this file using
So I have this method that should read a file and detect if the

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.