The easiest solution (which is deprecated) is to use ANSI code page for German:
setlocale(LC_ALL, "gr");
cout << "abcöäüÑ\n";
ofstream fout("ansi.txt");
fout << "abcöäüÑ\n";
This will work for some limited character sets, it's relatively safe if you stick to Western Latin characters. Maybe this is what you have done with your C code. It doesn't have much to do with saving the file in binary or text.
In Windows it is recommended to use Unicode with wide string functions. Example:
#include <iostream>
#include <string>
#include <fstream>
#include <codecvt>
#include <io.h>
#include <fcntl.h>
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);//wcout instead of cout
_setmode(_fileno(stdin), _O_U16TEXT); //wcin instead of cin
std::locale loc_utf16(std::locale(), new std::codecvt_utf16<wchar_t>);
std::wofstream fout(L"utf16.txt", std::ios::binary);
if(fout)
{
fout.imbue(loc_utf16);
fout << L'\xFEFF'; //insert optional BOM for UTF16
fout << L"abcöäüÑ ελληνική\r\n";
fout.close();
}
std::wifstream fin(L"utf16.txt", std::ios::binary);
if(fin)
{
fin.imbue(loc_utf16);
fin.seekg(2, std::ios::beg); //skip optional BOM if it were added
std::wstring ws;
while(std::getline(fin, ws)) std::wcout << ws << std::endl;
fin.close();
}
return 0;
}
The disadvantage with UTF16 is that programs on systems like Linux may have a hard time with this format. Some people will save the file in UTF8 format, because UTF8 is more familiar with other systems.
Windows itself is UTF16 based. So you have to read and display the input in UTF16. But you can read/write the file in UTF8 format. Example:
std::wcout << "enter:";
std::wstring sw;
std::wcin >> sw;
std::locale loc_utf8(std::locale(), new std::codecvt_utf8<wchar_t>);
std::wofstream fout(L"utf8.txt", std::ios::binary);
if(fout)
{
fout.imbue(loc_utf8);
fout << sw << L"\r\n";
fout << L"abcöäüÑ ελληνική\r\n";
}
Here the binary flag does matter.