Re: Upgrading unbuffered stream calls from <fstream.h> to <fstream>
outputstream << "Some text" << flush;
AliR.
"nmrcarl" <nmrcarl@discussions.microsoft.com> wrote in message
news:47DFB292-C9C6-4C69-9EF3-70052A179D6F@microsoft.com...
Hi
I am trying to maintain some old code written for VC6.0 using the
<fstream.h> header and library, to be compatible with <fstream> as used in
VS2003/2005, etc. The original project used a stream for "logging" -
recording messages during operation for debugging, monitoring, etc. In
order
to have the messages immediately available in a file, the original code
set
the stream to be "unbuffered". Thus, every character (or string) sent to
the
stream was almost immediately available for inspection in the output file.
The original project used the following code to accomplish this:
class DLLInterface CLogClass : public fstream , public CMyOtherClass
{
public:
int Init();
}
int CLogClass::Init()
{
// Clear all error bits and Set to Unbuffered output
close();
setbuf(NULL,0);
clear(0);
open( m_ExeFileName, ios::out|ios::trunc,
filebuf::sh_write||filebuf::sh_read);
errflag = rdstate();
if( ((errflag&ios::failbit)) ||
((errflag&ios::badbit )) ||
((errflag&ios::eofbit )) ) // File open failed
{
sprintf(s,"ERROR: Could Not Open Log File %s",FileName);
AfxMessageBox(s);
return(ERR_LogFileNotOpened);
}
else
{
// ...start sending messages to log file
}
return 0;
}
. . . .
// Elsewhere in the code
CLogClass MyLog;
. . . .
MyLog << "Log entry string \n";
. . . .
So far I have figured out how to convert "open()" to the new standard C++
library. But try as I might, I cannot get the output to be unbuffered.
The
buffering means that the output file lags the actual activity by dozens of
entries, which is pretty much useless. "setbuf()" in the old library
seems
to be an altogether different function from "setbuf()" in the new library,
or
from "basic_filebuf::setbuf()". If left unchanged, it causes an assertion
or
crash (the stream is undefined) with the new library. If I substitute
"rdbuf()->pubsetbuf(NULL,NULL);" nothing happens at all. Likewise if I
use
"rdbuf()->pubsetbuf((char*)rdbuf(),NULL);", even though this does pass the
correct stream buffer pointer.
Any suggestions?
Thanks!
--
C Gregory
Tecmag, Inc.