0

I'm trying to get a std::wstreambuf from a std::wstring, so I managed to do it like the following :

struct membuf : std::wstreambuf
{
   public:
     membuf(std::wstring begin, std::wstring end) {
     this->setg(&begin[0], &begin[0], &end[0]);
   }
};
std:wstring wstr = L"Im a random test\n";
membuf sbuf(&wstr[0], &wstr[wstr.size()]);

The thing is that sbuf is filled with "☐" I can't understand why.

Does anyone have an idea ?

bcperth
  • 2,191
  • 1
  • 10
  • 16
  • 1
    What is the reason for your own `membuf` implementation? What problem is it supposed to solve that can't be solved by the standard [`std::basic_stringbuf`](https://en.cppreference.com/w/cpp/io/basic_stringbuf) and [`std::basic_stringstream`](https://en.cppreference.com/w/cpp/io/basic_stringstream) (or the specific input/output variants)? – Some programmer dude Oct 19 '18 at 14:04
  • 1
    More related to your problem, your constructor doesn't take iterators (which is the usual) but string object *by value*. The variables `begin` and `end` are two totally *unrelated* strings. You can't use `end` as an end-pointer for the `setg` call. – Some programmer dude Oct 19 '18 at 14:06
  • Do you know this Q/A: [SO: Creating an input stream from constant memory](https://stackoverflow.com/a/13059195/7478597)? It provides a working sample to cheat from. (I know it's working as I used it myself to answer [SO: How to convert QByteArray to std::istream or std::ifstream?](https://stackoverflow.com/a/52492027/7478597).) ;-) – Scheff's Cat Oct 19 '18 at 14:32
  • This already exists in C++: it's called wstringbuf. – Etienne de Martel Oct 19 '18 at 15:39

0 Answers0