serialize ofstring to stream

G

Gernot Frisch

does this work:

struct XY
{
int crc; std::string filename;
}

std::eek:fstream& operator<<(std::eek:fstream& os, const XY& me)
{
os << me.crc;
os << me.filename;
return os;
}

std::ifstream& operator>>(std::ifstream& is, XY& me)
{
is>>me.crc;
is>>me.filename;
return is;
}

if fstream's are opened in binary mode?


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
K

Karl Heinz Buchegger

Gernot said:
does this work:

struct XY
{
int crc; std::string filename;
}

std::eek:fstream& operator<<(std::eek:fstream& os, const XY& me)
{
os << me.crc;
os << me.filename;
return os;
}

std::ifstream& operator>>(std::ifstream& is, XY& me)
{
is>>me.crc;
is>>me.filename;
return is;
}

if fstream's are opened in binary mode?

Why not.

There seems to be a miconception what 'binary mode' means.
It simply means that all characters are written as they
are in memory. No character replacement, like substituting
CR LF for CR or vice versa takes place. That's all what
binary does: Write to the stream target the same characters
the programm feeds to the stream. No more, no less, especially
no fancy conversions.
 
D

Dietmar Kuehl

Karl said:

There is no separator between the CRC and the filename.
As long as the filename does not start with a digit this
may be OK, otherwise it is not. Of course, you are
probably right about the misconception about "binary mode".
 
K

Konstantin Litvinenko

Hello, Gernot!
You wrote on Mon, 6 Dec 2004 16:14:49 +0100:

GF> does this work:

[Sorry, skipped]

GF> if fstream's are opened in binary mode?

Why not? But if you want to see binary representation of data you should not
use operator << , use ostream::write instead.

With best regards, Konstantin Litvinenko. E-mail: (e-mail address removed)
 

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

Similar Threads

std::format 1
reference to parent inherritance? 9
ifstream - nocreate - how to? 1
reference to array? 13
int(a) vs (int)a 19
operator overloading... 5
unreachable code? 4
std::list - access at index? 3

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top