Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > objectStream in C++?

Reply
Thread Tools

objectStream in C++?

 
 
Jens
Guest
Posts: n/a
 
      10-28-2004
Hi

Is there at class in C++ to make objectstreams or shall I make it yourself?

Thanks
Jens
 
Reply With Quote
 
 
 
 
Catalin Pitis
Guest
Posts: n/a
 
      10-28-2004

"Jens" <(E-Mail Removed)> wrote in message
news:4180be42$0$93475$(E-Mail Removed) k...
> Hi
>
> Is there at class in C++ to make objectstreams or shall I make it
> yourself?
>
> Thanks
> Jens


A common solution is to define streaming operators for stream in and out of
an object.

For example,

class A
{
[...]
template <typename E, typename T>
friend std::basic_ostream< E, T>& operator<<( std::basic_ostream< E, T>&
os, const A& obj)
{
os << obj.x;
return os;
}

template <typename E, typename T>
friend std::basic_istream< E, T>& operator>>( std::basic_istream< E, T>&
is, A& obj)
{
is >> obj.x;
return is;
}

private:
int x;
};

You have in STL file streams, string streams and you can find other kind of
streams (for sockets for example).

the usage is very simple:

A obj1, obj2;
std::cout << obj1 << obj2;

Catalin

P.S. The code is just a sketch. It may not compile.


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      10-28-2004
Jens wrote:

> Hi
>
> Is there at class in C++ to make objectstreams or shall I make it
> yourself?


That depends on what an "objectstream" is.

 
Reply With Quote
 
Jens
Guest
Posts: n/a
 
      10-28-2004
Catalin Pitis wrote:

>
> "Jens" <(E-Mail Removed)> wrote in message
> news:4180be42$0$93475$(E-Mail Removed). dk...
>> Hi
>>
>> Is there at class in C++ to make objectstreams or shall I make it
>> yourself?
>>
>> Thanks
>> Jens

>
> A common solution is to define streaming operators for stream in and out
> of an object.
>
> For example,
>
> class A
> {
> [...]
> template <typename E, typename T>
> friend std::basic_ostream< E, T>& operator<<( std::basic_ostream< E,
> T>&
> os, const A& obj)
> {
> os << obj.x;
> return os;
> }
>
> template <typename E, typename T>
> friend std::basic_istream< E, T>& operator>>( std::basic_istream< E,
> T>&
> is, A& obj)
> {
> is >> obj.x;
> return is;
> }
>
> private:
> int x;
> };
>
> You have in STL file streams, string streams and you can find other kind
> of streams (for sockets for example).
>
> the usage is very simple:
>
> A obj1, obj2;
> std::cout << obj1 << obj2;
>
> Catalin
>
> P.S. The code is just a sketch. It may not compile.

Hi

My problem is to write a vector<float> to a file and later read the file
into a vector<float>. Sorry I not where precise.

Jens
 
Reply With Quote
 
Catalin Pitis
Guest
Posts: n/a
 
      10-28-2004

"Jens" <(E-Mail Removed)> wrote in message
news:4180d795$0$93463$(E-Mail Removed) k...
> Catalin Pitis wrote:
>
>>
>> "Jens" <(E-Mail Removed)> wrote in message
>> news:4180be42$0$93475$(E-Mail Removed). dk...
>>> Hi
>>>
>>> Is there at class in C++ to make objectstreams or shall I make it
>>> yourself?
>>>
>>> Thanks
>>> Jens

>>
>> A common solution is to define streaming operators for stream in and out
>> of an object.
>>
>> For example,
>>
>> class A
>> {
>> [...]
>> template <typename E, typename T>
>> friend std::basic_ostream< E, T>& operator<<( std::basic_ostream< E,
>> T>&
>> os, const A& obj)
>> {
>> os << obj.x;
>> return os;
>> }
>>
>> template <typename E, typename T>
>> friend std::basic_istream< E, T>& operator>>( std::basic_istream< E,
>> T>&
>> is, A& obj)
>> {
>> is >> obj.x;
>> return is;
>> }
>>
>> private:
>> int x;
>> };
>>
>> You have in STL file streams, string streams and you can find other kind
>> of streams (for sockets for example).
>>
>> the usage is very simple:
>>
>> A obj1, obj2;
>> std::cout << obj1 << obj2;
>>
>> Catalin
>>
>> P.S. The code is just a sketch. It may not compile.

> Hi
>
> My problem is to write a vector<float> to a file and later read the file
> into a vector<float>. Sorry I not where precise.
>
> Jens


Here is some code

vector< float> vecToStore;
// ... initialize it

// Output to stream cout, with space delimiters
copy ( vecToStore.begin ( ), vecToStore.end ( ),
ostream_iterator<float> ( cout, " " ) );

vector< float> vecToLoad;

// Input from stream cin
copy( istream_iterator<float>( cin), istream_iterator<float>(),
vecToLoad.begin());

Again, it is just a sketch.

Catalin


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-28-2004
>
> Here is some code
>
> vector< float> vecToStore;
> // ... initialize it
>
> // Output to stream cout, with space delimiters
> copy ( vecToStore.begin ( ), vecToStore.end ( ),
> ostream_iterator<float> ( cout, " " ) );
>
> vector< float> vecToLoad;
>
> // Input from stream cin
> copy( istream_iterator<float>( cin), istream_iterator<float>(),
> vecToLoad.begin());
>


Should be

copy( istream_iterator<float>( cin), istream_iterator<float>(),
back_inserter(vecToLoad));

Also the code above uses a text representation of the floating point
numbers, which is good because its portable. But if you want a binary
representation, try this

vector< float> vecToStore;
vector< float>::size_type size = vecToStore.size();
out.write((char*)&size, sizeof size);
if (size > 0)
out.write((char*)&vecToStore[0], size*sizeof(float));

vector< float> vecToLoad;

vector< float>::size_type size;
in.write((char*)&size, sizeof size);
if (size > 0)
{
vecToLoad.resize(size);
in.write((char*)&vecToLoad[0], size*sizeof(float));
}

Again untested.

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use both ObjectStream and ByteArrayStream to access one file? ZelluX Java 5 10-24-2007 07:52 PM
Does Class casting survive an ObjectStream Gordon Beaton Java 5 11-01-2005 08:44 AM
ObjectStream issues espresso Java 4 06-19-2004 01:26 PM
Can't write certain objects through ObjectStream Peter Forster Java 4 02-27-2004 10:04 PM



Advertisments