iostream buffer and write to file

G

garyatusa

Hi, there,

I need to put a bunch of data to an iostream, or a buffer sequencially
for each object, and write them to a file in specific sequence. I
couldn't find any helpful information how to do that. If iostream is
not the best way, how to do it. I don't know the size of the object.
Your time and help are highly appreciated. Following is the structure
stripped off unnecessary parts:

class Base
{
int length;
virtual void Read();
virtual void Write();
};

class D1 : public Base
{
streambuf* m_stream; // or iostream, which one is better?
};

void D1::Read()
{
m_stream = new iostream();

streampos start = m_stream->tellp();
for (int i = 0; i < 10; i++) //write some data to the buffer
m_stream->put(i);
streampos end = m_stream->tellp();

CheckWhatWeWrite(m_stream, start, end); //suppose we need to check
}

void D1::Write(ofstream* fOut)
{
//fOut->rdbuf(m_stream); ??? //I need to append this to fOut, which
function should use?
}

Thanks lot in advance!
 
M

mlimber

Hi, there,

I need to put a bunch of data to an iostream, or a buffer sequencially
for each object, and write them to a file in specific sequence. I
couldn't find any helpful information how to do that. If iostream is
not the best way, how to do it. I don't know the size of the object.
Your time and help are highly appreciated. Following is the structure
stripped off unnecessary parts:

class Base
{
int length;
virtual void Read();
virtual void Write();
};

class D1 : public Base
{
streambuf* m_stream; // or iostream, which one is better?
};

void D1::Read()
{
m_stream = new iostream();

streampos start = m_stream->tellp();
for (int i = 0; i < 10; i++) //write some data to the buffer
m_stream->put(i);
streampos end = m_stream->tellp();

CheckWhatWeWrite(m_stream, start, end); //suppose we need to check
}

void D1::Write(ofstream* fOut)
{
//fOut->rdbuf(m_stream); ??? //I need to append this to fOut, which
function should use?
}

Thanks lot in advance!

You should generally prefer to use higher level abstractions than
streambufs unless you have to. I'm not sure exactly what you're trying
to do here, but you might be able to use a std::eek:stringstream. Just
stream the data into that object, and then when you are ready to save
it, do something like:

std::cout << myStringStream.str();

If you're trying to serialize and unserialize objects for persistence
or transmission, check out these FAQs:

http://www.parashift.com/c++-faq-lite/serialization.html

and consider Boost.Serialization
(http://boost.org/libs/serialization/doc/index.html).

Cheers! --M
 
G

garyatusa

Thanks for your reply.

I don't think ostrstream is fine in my case.

What I am going to do is:

1. save several data objects to a binary file sequencially, and
2. need to calculate the checksum for each of them before flushing.

There are BYTE, short, int data types in each object as well as some
pointers to other arrays. In computing the checksum, I need to read in
those data one by one in ULONG format, which is 4 bytes long, and sum
the ULONG data up. I could use fstream to put each object to the
stream, and read back to calculate the sum. However, I am wondering if
I can store the sequence of binary data in a stream in each object,
calculate the check sum in advance, and assembly them together in a
ofstream object finally. Is it possible?

The reason I tried iostream is that I thought it would be easy to flush
the content in iostream into ofstream. Is it true?

Thanks.
 
M

mlimber

Thanks for your reply.

I don't think ostrstream is fine in my case.

What I am going to do is:

1. save several data objects to a binary file sequencially, and
2. need to calculate the checksum for each of them before flushing.

There are BYTE, short, int data types in each object as well as some
pointers to other arrays. In computing the checksum, I need to read in
those data one by one in ULONG format, which is 4 bytes long, and sum
the ULONG data up. I could use fstream to put each object to the
stream, and read back to calculate the sum. However, I am wondering if
I can store the sequence of binary data in a stream in each object,
calculate the check sum in advance, and assembly them together in a
ofstream object finally. Is it possible?

The reason I tried iostream is that I thought it would be easy to flush
the content in iostream into ofstream. Is it true?

Thanks.

Please quote the message you are responding to so everyone can follow
the conversation. (In Google Groups, click "show options" and then
"Reply" in the revealed header to automatically quote.)

To answer your question: you could just use a std::vector<char> with a
simple front-end that accepts chars, shorts, ints, or whatever and
converts them to the appropriate number of chars. Then there'd be no
need to deal with streambufs at all. You could easily compute your
checksum through normal looping and array access, and then you can
easily dump it to a file with ofstream.write().

Cheers! --M
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top