Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / CodeWarrior / January 2004



Tip: Looking for answers? Try searching our database.

C++ file I/O won't flush

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Micah Koch - 27 Jan 2004 05:00 GMT
I was debugging something in my program and noticed my debug logs files
weren't getting flushed to disk.  I'm using C++ standard I/O calls to do
the logging.  I can reproduce it with the following program(I created my
test project with the Carbon C++ Console stationary):

#include <iostream>
#include <fstream>

int main()
{
  using namespace std;
  std::ofstream theFile("DebugLog.out");
  for(int i = 0; i < 150; ++i)
  {
     theFile << "Hello World, this is CodeWarrior! " << i << std::endl;
     theFile.flush();
  }
 
  char c;
  std::cin >> c;
 
 
  return 0;
}

If you check the log file before you press a key for the cin to finish
you'll see its not all flushed to file.  But after the program
essentially terminates it is flushed.  If I replace the C++ I/O with the
equivalent C routines it all works fine.  Am I missing something
obvious?  This is all with CW9.1, on OS X 10.3(Just tested with CW8.3
and it doesn't seem have this issue).  Thanks,
Micah
Howard Hinnant - 27 Jan 2004 14:00 GMT
> I was debugging something in my program and noticed my debug logs files
> weren't getting flushed to disk.  I'm using C++ standard I/O calls to do
[quoted text clipped - 28 lines]
> and it doesn't seem have this issue).  Thanks,
> Micah

This is our bug, sorry.  It has been fixed for 9.2 (not yet released).  
If you would like to patch your MSL, open <msl_c_filebuf>.  Add:

template <class charT, class traits>
int
c_filebuf<charT, traits>::sync()
{
   int result = base::sync();
   if (result >= 0)
       result = _CSTD::fflush(file_);
   return result;
}

anywhere outside of the class declaration.  Also add a declaration for
this function in the protected section:

protected:

   virtual int sync();

There is no need to recompile MSL C++ after this change.  But if you are
using precompiled headers you will need to rebuild them.

-Howard
Metrowerks
Micah Koch - 27 Jan 2004 19:45 GMT
Works like a charm.  Thanks Howard,
Micah

In article
<hinnant-523EAB.09002827012004@syrcnyrdrs-03-ge0.nyroc.rr.com>,

> <snip>
>
[quoted text clipped - 5 lines]
> -Howard
> Metrowerks
Hartwig Wiesmann - 29 Jan 2004 18:15 GMT
Hi Howard,

I do not know if I/O to "cout" also uses the "sync" function. At least
I/O with SIOUX and writing to "cout" also does not flush the buffer.

Hartwig

>>I was debugging something in my program and noticed my debug logs files
>>weren't getting flushed to disk.  I'm using C++ standard I/O calls to do
[quoted text clipped - 54 lines]
> -Howard
> Metrowerks
MW Ron - 29 Jan 2004 19:33 GMT
>Hi Howard,
>
>I do not know if I/O to "cout" also uses the "sync" function. At least
>I/O with SIOUX and writing to "cout" also does not flush the buffer.

I think Howard has more vacation time coming but I'll pass this on to
him and the MSL team.

Ron

>>>I was debugging something in my program and noticed my debug logs files
>>>weren't getting flushed to disk.  I'm using C++ standard I/O calls to do
[quoted text clipped - 54 lines]
>> -Howard
>> Metrowerks

Signature

Metrowerks, maker of CodeWarrior   -  "Software Starts Here"  
Ron Liechty - MWRon@metrowerks.com - <http://www.metrowerks.com>

Howard Hinnant - 29 Jan 2004 20:42 GMT
> I do not know if I/O to "cout" also uses the "sync" function. At least
> I/O with SIOUX and writing to "cout" also does not flush the buffer.

Yes, you are correct, cout has the same bug.  To fix, open
<msl_consolebuf>.  There are two classes in this header:  
console_inputbuf and console_outputbuf.  Only console_outputbuf needs
patching:

template <class charT, class traits>
int
console_outputbuf<charT, traits>::sync()
{
   int result = base::sync();
   if (result >= 0)
       result = _CSTD::fflush(file_);
   return result;
}

-Howard
Metrowerks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.