473,473 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

objectStream in C++?

Hi

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

Thanks
Jens
Jul 22 '05 #1
5 1418

"Jens" <je*********************@tiscali.dk> wrote in message
news:41***********************@dread15.news.tele.d 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.
Jul 22 '05 #2
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.

Jul 22 '05 #3
Catalin Pitis wrote:

"Jens" <je*********************@tiscali.dk> wrote in message
news:41************************@dread15.news.tele. 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
Jul 22 '05 #4

"Jens" <je*********************@tiscali.dk> wrote in message
news:41***********************@dread15.news.tele.d k...
Catalin Pitis wrote:

"Jens" <je*********************@tiscali.dk> wrote in message
news:41************************@dread15.news.tele. 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
Jul 22 '05 #5
>
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
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
2
by: Gabriel Gudenus | last post by:
Hi Group! I am just working on a client-server network application which exchanges different Message Classes. (ObjectInputStream and ObjectOutputStream) Now i also would like to send a file...
1
by: Mike Frayn | last post by:
Hello again, Okay, I was reading the following tutorial on serialization of user-defined classes, and it seems pretty straightforward: http://www.sys-con.com/story/?storyid=44199 That...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.