Prev: What C++0x features are relevant to you?
Next: omission of "virtual" in overridden method declarations in derived classes
From: Rem on 21 Feb 2010 13:21 I have a third party class X that stores and loads its objects using std::ostream and std::istream classes. At the same time I'm using X objects inside an environment that only allows me to save chunks of memory c-style. What I would like to do then is to store X instance in std::iostream instance and from that get the underlying array of chars. But the problem is I don't know how to determine to size of the buffer needed - there is no method of std::istream that returns the number of chars used, is it? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Michael Doubez on 21 Feb 2010 21:35 On 22 f�v, 07:21, Rem <therealr...(a)gmail.com> wrote: > I have a third party class X that stores and loads its objects using > std::ostream and std::istream classes. At the same time I'm using X > objects inside an environment that only allows me to save chunks of > memory c-style. What I would like to do then is to store X instance in > std::iostream instance and from that get the underlying array of > chars. Do you mean, you want to use a X has a sink for your iostream ? A minimal implementation is creating a structure inheriting streambuf and redefining the underflow() member function. > But the problem is I don't know how to determine to size of the > buffer needed - there is no method of std::istream that returns the > number of chars used, is it? std::istream::gcount() returns the number of chars extracted by the last unformatted read(). std::istream::tellg() may or may not be accurate depending on how you use your stream. -- Michael -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bart van Ingen Schenau on 21 Feb 2010 21:35 On Feb 22, 7:21 am, Rem <therealr...(a)gmail.com> wrote: > I have a third party class X that stores and loads its objects using > std::ostream and std::istream classes. At the same time I'm using X > objects inside an environment that only allows me to save chunks of > memory c-style. What I would like to do then is to store X instance in > std::iostream instance and from that get the underlying array of > chars. But the problem is I don't know how to determine to size of the > buffer needed - there is no method of std::istream that returns the > number of chars used, is it? Not in std:;istream no, but that class is designed to be used as a base-class. You could use (the deprecated) strstream or std::stringstream classes to let X load/store itself to/from respectively a memory buffer or a std::string object. If your problem is knowing where to continue in the buffer after X hasa consumed its portion, you could extract the remainder of the stream into a std::string object using getline(). Bart v Ingen Schenau -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 21 Feb 2010 21:35 Rem wrote: > I have a third party class X that stores and loads its objects using > std::ostream and std::istream classes. At the same time I'm using X > objects inside an environment that only allows me to save chunks of > memory c-style. What I would like to do then is to store X instance in > std::iostream instance and from that get the underlying array of > chars. But the problem is I don't know how to determine to size of the > buffer needed - there is no method of std::istream that returns the > number of chars used, is it? Have you taken a look at std::stringstreams? Those read/write from a string instead of a file. If that doesn't work or doesn't perform well enough, you could also write a streambuffer that directly uses the underlying C-style API. Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Davis King on 22 Feb 2010 05:57
On Feb 22, 1:21 am, Rem <therealr...(a)gmail.com> wrote: > I have a third party class X that stores and loads its objects using > std::ostream and std::istream classes. At the same time I'm using X > objects inside an environment that only allows me to save chunks of > memory c-style. What I would like to do then is to store X instance in > std::iostream instance and from that get the underlying array of > chars. But the problem is I don't know how to determine to size of the > buffer needed - there is no method of std::istream that returns the > number of chars used, is it? Have you looked at the std::ostringstream and std::istringstream objects? I think they might be just what you are looking for. They allow you to get a copy of the data in the stream in the form of a std::string. Cheers, Davis -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |