Cannot read from a file using std::wifstream

  • Thread starter Thread starter Juan Dent
  • Start date Start date
J

Juan Dent

Guest
Hi,

I know a file uses 16 bit chars because I have seen it in the binary editor of Visual Studio 2017. So I am using std::wifstream to open it. However when I read with getline, I get garbage (bunch of symbols looking japanese or so).


Then I tried to imbue a facet like this:


std::wifstream in{ m_headerFile.c_str() };

// apply facet
in.imbue(std::locale(in.getloc(), new std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>));



and this worked (now I get wchar_t characters but no "japanese" symbols.

The problem is I don't know why I needed to imbue this facet and why this one in particular. It was sort of trial and error. I had expected that the reading of the file would be trivial but it wasn't. By the way, the file is a text file (actually generated by Visual Studio 2017 when I created an MFC app), which is very strange...


Can someone explain to me what is happening here? Also, is there a way to ASK the fie to give you the encoding it uses so I can open it with the correct encoding?


See, I copy from this file to another and when I try to compile the other as part of my solution, well I get problems like this:


warning C4335: Mac file format detected: please convert the source file to either DOS or UNIX format
1>c:\users\juan dent\source\repos\mfc\samplecformview\cviewsubclass.cpp(2): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?


Thanks,

Juan


Juan Dent

Continue reading...
 
Back
Top