Re: Upgrading unbuffered stream calls from <fstream.h> to <fstream

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Ok here is what I came up with, and it seems to be doing the trick:

ofstream stream;
stream.open("C:\\Temp.txt",ios_base::out);
stream.rdbuf()->pubsetbuf(0,0);
stream << "This is a test!";

AliR.

"AliR (VC++ MVP)" <AliR@xxxxxxxxxxxxx> wrote in message
news:I8FDi.24029$eY.7343@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
There is a setbuf method for basic_streambuf. But it is protected.
Haven't figured out how to use it yet.

Still looking for solution.

AliR.

"nmrcarl" <nmrcarl@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:AB15B488-D783-472F-935D-818715964650@xxxxxxxxxxxxxxxx
AliR

Thanks. I had thought about that (or "endl") - and I'll do it if I can't
find a simpler fix. But there are a hundred or more files, each of which
uses the outputstream object many times - a lot of edits. I was hoping
for a
"simpler" solution. . . .

--
C Gregory
Tecmag, Inc.



"AliR (VC++ MVP)" wrote:

outputstream << "Some text" << flush;

AliR.


"nmrcarl" <nmrcarl@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:47DFB292-C9C6-4C69-9EF3-70052A179D6F@xxxxxxxxxxxxxxxx
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.








.



Relevant Pages

  • Re: pipedstreams
    ... time readInt returns values that has never been written to the stream ... to put the wrapped streams into your Comm object. ... catch (IOException e){System.err.println("An error occured ... public synchronized void send(int dest, ...
    (comp.lang.java.programmer)
  • Re: Sending XML data via socket
    ... I looked int a link you have provided. ... // Get a stream object for reading and writing ... Resize the buffer, put in the byte we've just ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: adapting getline
    ... int getline; ... The function reads characters from the input stream stream and stores them ...
    (comp.lang.c)
  • Re: how to remove code duplication
    ... the log directory should containing the same files as the out ... exit(EXIT_FAILURE); ... int main ... void process_log_data(FILE *stream) ...
    (comp.lang.c)
  • Re: Why doesnt std::cin choke on this?
    ... when I was writing a user-driven test program for a data structure I ... You read an int. ... it simpler then checking the stream for it's ... read in a string, this becomes trivial. ...
    (comp.lang.cpp)