0

I am trying to write into a file wchar_t(s) from value 0 to max (FFFF or 65,535). The process is successful but when I opened the output file it ended at value 256 with no symbol. Why does this happen?

I've tried making the program sleep and instead wchar_t I used wstring (wstring is written instead of the wchar_t directly) but it all ended the same way.

//the loop

for (int y = 0; y < 256; y++) {
// "out" is declared as wofstream
            out << y + 1 << ":\t";
            for (int x = 0; x < 256; x++) {
                wchar_t c = ((y * 256) + x);
                wstring u;
                u = c;
                int l = (y * 256) + x;
                out << "[" << l << " :: " << u << "]";
            }
            out << "\n";
        }

The last in the file should be "[65535 :: (symbol here)]" but it ended at "[256 :: (blank and no closing bracket)"

  • Sometimes 0xFF is mistakenly used as the EOF character, sewing chaos and disorder across the universe. The file reader sees the FF in the file, thinks, "Well. That's the end of that." and stops reading. What are you using to read the file? – user4581301 May 17 '19 at 17:18
  • 1
    please provide a [mcve], at a guess you are on windows and not opening your stream in binary mode – Alan Birtles May 17 '19 at 17:20
  • @user4581301 ahhh I see I'll try to fix that issue – Silverous Black May 17 '19 at 18:20
  • @Alan Birtles, yes I am using the Windows Notepad to view it. So I guess it's still vogue, is it? The code I showed, I mean. – Silverous Black May 17 '19 at 18:23
  • yes it is, a [mcve] should be copy and pastable into a compiler and show the problem. – Alan Birtles May 17 '19 at 18:54
  • 2
    iostream rarely fails to disappoint, wofstream doesn't actually write Unicode-encoded text by default. That's a separate detail, you have to select the desired encoding format explicitly. There's more than 1 choice: https://stackoverflow.com/q/3950718/17034 – Hans Passant May 17 '19 at 19:17
  • Possible duplicate of [Windows Unicode C++ Stream Output Failure](https://stackoverflow.com/questions/9859020/windows-unicode-c-stream-output-failure) – phuclv May 18 '19 at 06:46
  • [wostream fails to output wstring](https://stackoverflow.com/q/11610583/995714), [why std::wofstream do not print all wstring into file?](https://stackoverflow.com/q/18226745/995714) – phuclv May 18 '19 at 06:47

0 Answers0