470,849 Members | 901 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

iostream buffer and write to file

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!

May 15 '06 #1
3 6587
ga*******@gmail.com wrote:
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::ostringstream. 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-lit...alization.html

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

Cheers! --M

May 15 '06 #2
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.

May 15 '06 #3
ga*******@gmail.com wrote:
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

May 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Garrett Kajmowicz | last post: by
7 posts views Thread by google | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.