0

My wofstream is truncating when trying to write wide characters to the file.

my_file << L"something";

wstring foo = //get a value from the registry ..

// foo contains 您好

my_file << foo;

my_file << "stuff that will never be seen";
ST3
  • 8,826
  • 3
  • 68
  • 92
Bluebaron
  • 2,289
  • 2
  • 27
  • 37
  • What's the declaration of my_file? – FailedDev Oct 26 '11 at 18:30
  • 1
    Have you tried flushing and closing the `wofstream` after you write everything? – rturrado Oct 26 '11 at 18:41
  • yup. God why is this so effing difficult ... I have literally been dealing with this issue for weeks now. Weeks and weeks. – Bluebaron Oct 26 '11 at 18:51
  • What's the type of the registry key you are trying to get? – rturrado Oct 26 '11 at 19:05
  • just a string value. you'll get the same problem by just trying wcout << "something好stuff that will never be seen"; – Bluebaron Oct 26 '11 at 19:13
  • 1
    Can you give us a complete program that illustrates the problem. – David Heffernan Oct 26 '11 at 20:45
  • possible duplicate of [File truncates after hitting a wide character](http://stackoverflow.com/questions/7906713/file-truncates-after-hitting-a-wide-character) Different type of stream, same problem. Don't keep asking the same question if the answers don't fit in your wagon. – rubenvb Oct 26 '11 at 21:05

1 Answers1

0

The badbit (or other error bit) is being set in your stream, rendering it useless for any operation afterwards. This is probably on the border of implementation defined behavior (especially for wchar_ts). It just won't work the way you want it to.

The largest probability for the error is that the characters don't fit in one 2-byte wchar_t, making Microsoft's STL choke.

rubenvb
  • 74,642
  • 33
  • 187
  • 332