N
N
Nyarogami2020-07-21 11:45:24
C++ / C#
Nyarogami, 2020-07-21 11:45:24

How to change the encoding when writing data to a file?

I'm trying to write a word in Cyrillic into a file, but in the file itself I don't have the word "Test" but it's not clear what. Understood that business in the coding, searched in Google all advised to use SetConsoleCP (1251); but that didn't help me.

#include <iostream>
#include <Windows.h>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    setlocale(LC_ALL, "ru");

    string path = "C:\\Users\\Nyarogami\\Desktop\\piFile.txt";

    ofstream myFile;
    myFile.open(path, fstream::in | fstream::out | fstream::app);
    SetConsoleCP(1251);
    myFile << "Тест";
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2020-07-21
@res2001

You need to convert the text to the encoding you want to see in the file.
Or just write the source code in this encoding.
In general, find an editor that understands different encodings and use it to view the text. At the stage of learning a language, it may not make sense to delve into the nuances of encodings. I can recommend Notepad++ or the built-in editor Far manager.
In Windows, this is quite confusing.
But the encodings in the file are still a simple case.
When you try to write Cyrillic characters to the console, then you can fully feel the depth of the question, if, of course, you have enough patience for this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question