Hi
Is there at class in C++ to make objectstreams or shall I make it yourself?
Thanks
Jens 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.
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.
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
"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
> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |