1

This is my code (without error checking):

std::wfstream wfs("C:\\testfile.txt");
wfs.setf(std::ios_base::binary);
std::wstringstream wss;
wss << wfs.rdbuf();
wfs.seekp(0);
wfs << L"new";

Now, the first three characters in the file are replaced with "new". However, I would like "new" to be the only contents of the file.

How do I achieve that?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
  • 1
    `setf` takes format flags as an argument, not open mode flags. You want `std::wfstream wfs("C:\\testfile.txt", std::ios_base::binary)`. – David G Mar 15 '16 at 18:45
  • Oh, then I also need to copy the default arguments such as `std::ios::in | std::ios::out | std::ios::binary` right? – Felix Dombek Mar 15 '16 at 18:47
  • 1
    No, they're automatically specified in `fstream`. – David G Mar 15 '16 at 18:48
  • Please don't add answers to the question. If you want to answer your own question, that is fully possible by adding a new answer. The question box is just for the actual question. – default Mar 15 '16 at 18:53
  • Is that really a duplicate? That question specifies the read/trunk needs to be "atomic". – Galik Mar 15 '16 at 19:15
  • 1
    This is possible using the new [Filesystem Technical Specification](http://en.cppreference.com/w/cpp/experimental/fs): **See:** http://stackoverflow.com/a/36020320/3807729 – Galik Mar 15 '16 at 19:31

1 Answers1

0

Try this

wfs.setf(std::ios_base::binary,std::ios_base::trunc);
Samer
  • 1,923
  • 3
  • 34
  • 54