By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,441 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,441 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ka****@lycos.com 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

Nov 8 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.