iostream buffer and write to file

Discussion in 'C++' started by garyatusa@gmail.com, May 15, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. mlimber Guest

    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::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
     
    mlimber, May 15, 2006
    #2
    1. Advertising

  3. Guest

    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, 2006
    #3
  4. mlimber Guest

    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
     
    mlimber, May 16, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Tiger
    Replies:
    10
    Views:
    5,609
  2. ai@work
    Replies:
    9
    Views:
    549
    Ron Natalie
    Dec 16, 2004
  3. S. Nurbe

    iostream + iostream.h

    S. Nurbe, Jan 14, 2005, in forum: C++
    Replies:
    7
    Views:
    780
    red floyd
    Jan 15, 2005
  4. red floyd
    Replies:
    3
    Views:
    537
    Dietmar Kuehl
    Mar 8, 2005
  5. radnoraj
    Replies:
    6
    Views:
    2,826
    Chris \( Val \)
    Apr 30, 2005
Loading...

Share This Page