to synchronise a c++ stream from a queue to write to disk

Discussion in 'C++' started by kavi81@lycos.com, Nov 4, 2005.

  1. Guest

    Hi,
    I have written a c++ code which is a message queue.
    I have an Enqueue and a Dequeue.

    Here is the code for enqueue:


    ##########################################################
    Enqueue
    ##########################################################
    bool FileMessageQueue::Enqueue( const string& in_message )
    {
    I_TRACE( LVL_NORMAL, "FileMessageQueue::Enqueue( const string& )" );

    bool out = false;

    ofstream sout( _fileName.c_str(), ios::app | ios::ate );

    if ( true == sout.good() )
    {
    sout << in_message << endl;
    }
    else
    {
    I_LOG( LM_ERROR, "FileMessageQueue - failed opening output stream
    for enqueue on file: '%s' for message: '%s'.\n", _fileName.c_str(),
    in_message.c_str() );
    }

    out = sout.good();

    sout.close();

    return out;
    }

    #######################################################
    How can I make this enqueue to write to disk? I need to write 1st line
    of enque to write to disk and then 1st line of dequeue to write to
    disk.

    Any suggestions would be great.

    Thanks
     
    , Nov 4, 2005
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > Hi,
    > I have written a c++ code which is a message queue.
    > I have an Enqueue and a Dequeue.
    >
    > Here is the code for enqueue:


    Here are some suggestions for improvements:

    >
    >
    > ##########################################################
    > Enqueue
    > ##########################################################
    > bool FileMessageQueue::Enqueue( const string& in_message )
    > {
    > I_TRACE( LVL_NORMAL, "FileMessageQueue::Enqueue( const string& )" );
    >
    > bool out = false;


    This variable is rendered unnecessary with the suggestions below, but
    in any case, you shouldn't declare it until you need to use it, which
    happens after the if-else.

    >
    > ofstream sout( _fileName.c_str(), ios::app | ios::ate );


    ofstream sout( _fileName.c_str(), ios::app ); // ios::app implies
    ios::ate

    >
    > if ( true == sout.good() )


    if( sout ) // Best practice: checks for all failures

    or

    if( sout.good() ) // ==true is implied

    > {
    > sout << in_message << endl;
    > }
    > else
    > {
    > I_LOG( LM_ERROR, "FileMessageQueue - failed opening output stream
    > for enqueue on file: '%s' for message: '%s'.\n", _fileName.c_str(),
    > in_message.c_str() );
    > }
    >


    return bool( sout ); // Returns the state of the stream
    // and closes it via the destructor
    }

    > out = sout.good();
    >
    > sout.close();


    Unnecessary: done by the destructor automatically.

    >
    > return out;
    > }
    >
    > #######################################################
    > How can I make this enqueue to write to disk?


    What do you mean? It is writing the message to the disk assuming no
    error occurs.

    > I need to write 1st line
    > of enque to write to disk and then 1st line of dequeue to write to
    > disk.


    You mean you want your I_TRACE message to write to disk? That's simple:
    just make the I_TRACE function write its parameter to some file similar
    to the enqueue function. If that's not what you meant, please clarify.

    >
    > Any suggestions would be great.
    >
    > Thanks


    Cheers! --M
     
    mlimber, Nov 8, 2005
    #2
    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. Jas Shultz
    Replies:
    0
    Views:
    987
    Jas Shultz
    Dec 3, 2003
  2. Simon
    Replies:
    0
    Views:
    379
    Simon
    Sep 9, 2003
  3. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    712
    Russell Warren
    Jun 27, 2006
  4. Replies:
    12
    Views:
    534
    santosh
    Nov 15, 2006
  5. Kris
    Replies:
    0
    Views:
    517
Loading...

Share This Page