Reading Cyrillic from a C file#

You need to read the file in which there are Cyrillic characters, when output to the screen, all clerical characters are displayed with question marks. Here is how I read the text from the file:

 static void Main(string[] args)
        {

            string path = @"C:\Users\USER\Desktop\words\worxds.txt";

            using (FileStream fstream = File.OpenRead(path))
            {
                // преобразуем строку в байты
                byte[] array = new byte[fstream.Length];
                // считываем данные
                fstream.Read(array, 0, array.Length);
                // декодируем байты в строку
                string textFromFile = System.Text.Encoding.Default.GetString(array);
                Console.WriteLine($"Текст из файла: {textFromFile}");
            }

        }

Is there a way to correctly return the Cyrillic alphabet after reading it in the console?

Author: Ncado, 2020-06-15

2 answers

You need to change the file encoding to UTF-8.

 1
Author: Ncado, 2020-06-15 22:21:56

Use Unicode. If you encode anything other than English (American), then always use Unicode.

Russian Russian characters can also be used in 1251 (ASCII is 7-bit, +1 bit for Russian characters), but it is only used in Russian texts, and you can insert any characters in the 1st text in Unicode.

var text = File.ReadAllText("path/to/file", Encoding.Unicode);
 1
Author: return, 2020-06-18 05:57:28