I have the following program that takes the first line from a file f1.txt
and copies it to another file, f2.txt
.
#include <fstream>
#include <string>
int main()
{
std::basic_ifstream<char32_t> fin("f1.txt");
std::basic_ofstream<char32_t> fout("f2.txt");
std::u32string s;
std::getline(fin, s);
fout << s;
}
The problem is that it throws an exception:
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
The getline
function seems to be the problem, because if I remove it, the problem goes away:
#include <fstream>
#include <string>
int main()
{
std::basic_ifstream<char32_t> fin("f1.txt");
std::basic_ofstream<char32_t> fout("f2.txt");
std::u32string s = U"π";
//std::getline(fin, s);
fout << s;
}
I tried changing the locale, but it didn't help:
std::locale::global(std::locale("en-US.UTF-8"));
What is the problem?