objectStream in C++?

J

Jens

Hi

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

Thanks
Jens
 
C

Catalin Pitis

Jens said:
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.
 
J

Jens

Catalin said:
Jens said:
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
 
C

Catalin Pitis

Jens said:
Catalin said:
Jens said:
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
 
J

John Harrison

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top