Thread: fflush() not working with Windows 7

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90

    fflush() not working with Windows 7

    << split from old thread Do I need to wait in between fclose() and fopen()? >>
    We have a number of applications writing sequential files to local drives - either to memory sticks or (still) to floppies for machine control.

    We are finding that fflush() does not appear to be working with Windows 7 PC's, and on closing a file, not everything is written. Usually it is at the end of the file, but not every time. We have tried multiple fflush() commands to no effect.

    If we write to the server or any non-Windows7 PC there are no problems.

    Is this a known bug and is there a fix?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which compiler (and version) are you using?

    Is it something reasonably new and maintained, or is it some decade old fossil?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90
    We use C++ 6.0 currently.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90
    Is undefined behaviour a euphemism for Microsoft?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well at least it's only 1 decade old, and not two

    Have you considered upgrading to something more recent perhaps?
    For one thing, you'll get a more up to date C-Runtime library that might actually be compatible with your new OS.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90
    I'm a great believer that if it's not broken don't fix it. Moving to new compilers often creates more problems than it fixes. We've allocated 3 months just to fix the problems that will arise from allowing 64 bit compatability.

    Anyway this is a Windows 7 problem - I know from a friend that it doesn't work with the latest version on his applications, although that hasn't helped, because his fix - writing to the server then copying back will mean a lot of changes, and I'd rather fix it in the low level library.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    "Anyway this is a Windows 7 problem"

    So have you looked for information on the following topics for Windows 7:

    Optimize for quick removal
    Optimize for performance
    Safely Remove Hardware

    From what little I have found (I have not looked for much), these features interact with each other so you will want to understand those interactions.
    Last edited by pheininger; 08-11-2010 at 10:31 AM. Reason: Indicate that I have not looked for much.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But you've already broken it by moving to a new OS.

    > Moving to new compilers often creates more problems than it fixes.
    Yes, it's a good way of flushing out a lot of bugs in your code.

    Moving from compiler 1 to compiler 2 involves the most pain. However, having done that, your code will be in much better shape so that by the time compiler 3 rolls around, it really isn't that difficult.

    If you're writing software for a living, you really need more than "well it compiles and it doesn't crash".
    How many warnings does the compiler generate? If it isn't zero, then you're doing it wrong.

    An investment in some code analysis tools perhaps. Coverity is pretty expensive, but PC-Lint pays for itself the first time it finds a bug you would otherwise have spent a day looking for.

    If you've never used such tools before, it can be a real eye opener.

    Software Quality and Security Analysis | Coverity
    Gimpel Software Home Page for PC-lint and FlexeLint for C/C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by johnggold View Post
    I'm a great believer that if it's not broken don't fix it. Moving to new compilers often creates more problems than it fixes.
    And yet moving to a new OS doesn't?!?!
    You should find that if you stay up to date with compilers that switching OSes gives at least as much pain, if not more. As others say, once you've switched the first time you find most of your bugs and then next time it's easy. And yes I'm talking about a very large project here.

    Anyway VS6 is broken. You tend to have to go to great lengths to work around its problems to try and get perfectly good sensible and normal code working with it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90
    I'm really a bit disappointed in the replies.

    I'm an application programmer and I still remember that the purpose of software is to do a job, not to be constantly changing for changes sake.

    The kernel of the software we develop is proven over 20 years. We fix ALL compiler errors, which occur rarely in our code anyway.

    It seems that no-one wants to answer the question I started with - how to get around fflush not working in Windows 7 even when it reports it IS working.

    The problem occurs on all local drives both removable and non-removable i.e drive C.

    Can someone please answer my question instead of talking about different compiler versions - already eliminated as the problem before I started posting.

    Thank you.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by pheininger View Post
    "Anyway this is a Windows 7 problem"

    So have you looked for information on the following topics for Windows 7:

    Optimize for quick removal
    Optimize for performance
    Safely Remove Hardware

    From what little I have found (I have not looked for much), these features interact with each other so you will want to understand those interactions.
    @johnggold:
    And, did I miss your reply to the above topics?

  13. #13
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90
    "Optimize for quick removal
    Optimize for performance
    Safely Remove Hardware"

    Checked all three. 1, 3 refer to removable media. Problems occurs on any local drive regardless of whether removable devices are or are not in use.

    The Windows 7 error can be reproduced on both Home and Professional versions of the OS, and the Home version has been stripped of all unwanted programs - we always do this.

    I'm still waiting for someone to talk about fflush() and a workaround that we can roll out to our Customers with confidence.

    Thanks.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Did you try to use the "c" option during file opening; You do know that you really should work with MS on a support forum if you really what an answer. social.answers.microsoft.com might not be the fastest site to try.

    _fdopen, _wfdopen (CRT)
    c

    Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90
    Always have used commit flag - see code fragment

    Code:
    if( ( fp = fopen( (const char *)filename, "w+bc" ) ) == NULL )
    	{
                       RESULT2 = 2;
    
                        fperror( filename, RESULT2 );

    I really need to find out what's going wrong with the fflush() command, and how to force it to act. If fflush() returned an error I could at least loop for a bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do I need to wait in between fclose() and fopen()?
    By patiobarbecue in forum C Programming
    Replies: 7
    Last Post: 07-06-2010, 04:33 PM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  3. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  4. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  5. Replies: 3
    Last Post: 08-18-2004, 07:39 AM