File Buffering Can Hide Out-of-Disk-Space Condition

Last reviewed: July 17, 1997
Article ID: Q57889
5.10 6.00 6.00a 6.00ax 7.00 8.00 8.00c | 1.00 1.50 1.51 1.52
MS-DOS                                 | WINDOWS
kbprg

The information in this article applies to:

  • The C Run-time (CRT) included with:

        - Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, and 6.0ax
        - Microsoft C for OS/2, versions 5.1, 6.0, and 6.0a
        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and
          1.52
    

SUMMARY

In Microsoft C, if disk space is exceeded, file buffering fails when buffering is done by the stream I/O functions. For example, if a file buffer for a stream is set to 10K by setvbuf() but only 2K of disk space is available, all data above 2K written to the buffer will be lost. This problem can occur even when setvbuf() and fwrite() return successful return codes.

MORE INFORMATION

When there is less space on the disk than there is in the stream buffer, all stream I/O functions will seem to work properly until the stream buffer is filled [for example, fwrite() returns the number of bytes written as if it were successful]. However, the status of these functions is valid only for the data going to the buffer and is not reflected in the file that is written to the disk.

The problem is due to the existence of both the C run-time buffers and the MS-DOS buffers. Only when the MS-DOS buffers try to write to disk does it become evident that the disk is full. Then, the next return value from fwrite() will indicate failure.

The following is the series of events that leads to the loss of data with buffered stream I/O functions:

  1. The stream is opened with fopen().

  2. Buffering is set on the stream, either 512 bytes default or the number of bytes selected by the user with setvbuf().

  3. There is less disk space than the size of the buffer set by step 2.

  4. Bytes are written to the file [for example, fwrite()], with successful return codes.

  5. The buffer is filled and then the stream I/O function attempts to write all the data to MS-DOS.

  6. The bytes that can fit on the disk are written and all remaining data that was in the buffer is lost.

  7. Successive calls to write data to the file fail.

The following are possible workarounds:

  1. Turn buffering off by setting the file buffer to NULL, using setvbuf().

  2. Use nonbuffered I/O functions, such as open(), read(), and write().

  3. Check the result of closing the stream with fclose(), which flushes all the buffers associated with the given stream prior to closing.

  4. Set buffering to the same size as the records that are being written. The third workaround will force fwrite() to return a "failure" return code at a point where the program can easily recover because the program knows exactly which records were successfully written to disk and which ones were not.


Additional reference words: kbinf 6.00 6.00a 6.00ax 7.00 1.00 1.50 1.51
1.52
KBCategory: kbprg
KBSubcategory: CRTIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.