0

I have two streams, one to read from and one to write to (diff. files).

std::wofstream origin;
std::wifstream attach;

origin.open(m_SourceFile, std::ios_base::app | std::ios_base::binary);
attach.open(csAttachFilename, std::ios_base::in | std::ios_base::binary);

Appending data to the file works fine, until it gets to a write operation with a std::streamoff variable. Taking into account that it is a std::wofstream, I output the variable "normally" as I did it already a few times in this function.

//[...]
origin.seekp(0, std::ios_base::end); //Go to the end
std::streamoff nOffSet = origin.tellp();

//int nFilenameLength = ...;
origin.write(reinterpret_cast<wchar_t*>(&nFilenameLength), sizeof(nFilenameLength) / sizeof(wchar_t)); //int
//Nothing wrong here
//[...] write more

attach.close();
auto c1 = origin.bad(); //false
origin.write(reinterpret_cast<wchar_t*>(&nOffSet), sizeof(nOffSet) / sizeof(wchar_t));
auto c2 = origin.bad(); //true
//[...] write more

What could cause this issue ?
Note that this works fine if I use a std::ofstream instead.

User9123
  • 53
  • 7
  • I can't replicate the error and you haven't provided a complete example, but in any case trying to binary write `wchar_t`'s like this is [undefined behavior](http://stackoverflow.com/a/7005988/657267), stick to `std::ofstream` and `char`s. – user657267 May 09 '16 at 07:52
  • @user657267 I see. But would there be another way to write it correctly with `std::wofstream`, aside from my "solution" ? (not `origin << nOffset`) – User9123 May 09 '16 at 11:59

0 Answers0