PRB: CFile::SetStatus Sets Archive Bit when Not Desired

ID: Q117832

7.00 | 1.00 1.50 MS-DOS | WINDOWS kbprg kbprb

The information in this article applies to:

SYMPTOMS

Attempting to clear the archive bit of a file when the archive bit is already cleared causes the archive bit to be set.

CAUSE

This is expected behavior that results when the m_mtime member of the CFileStatus structure is not set to 0, indicating that the time should be updated. Because updating the time has the side effect of setting the archive bit for a file, this action takes precedence over clearing the archive bit when the bit was clear to begin with.

RESOLUTION

1. If you want to clear only the archive bit, set the m_mtime member

   to 0, which indicates that you do not want to change the file time:

      CFile::GetStatus("TEMP.DAT",fs);
      fs.m_mtime=0;
      fs.m_attribute = fs.m_attribute & !CFile::archive;
      CFile::SetStatus("TEMP.DAT",fs);

2. Check to see whether the archive bit is clear instead of making a
   redundant call to clear it:

      CFile::GetStatus("TEMP.DAT",fs);
      if(fs.m_attribute & CFile::archive)
      {
        fs.m_attribute = fs.m_attribute & !CFile::archive;
        CFile::SetStatus("TEMP.DAT",fs);
      }

MORE INFORMATION

To duplicate this behavior, create a test file called TEMP.DAT and run the sample code below:

Sample Code

// Using the MS-DOS ATTRIB command, watch the archive bit toggle
// after each time you run the program.

   #define _DOS
   #include <afx.h>

   CFileStatus fs;

   void main(void) {
     CFile::GetStatus("TEMP.DAT",fs);
     fs.m_attribute = fs.m_attribute & !CFile::archive;
     CFile::SetStatus("TEMP.DAT",fs);
   }

This behavior does not occur with Visual C++ 32-bit Edition.

REFERENCES

The possible side effects of m_mtime on file attributes are discussed in the "Class Library Reference" for Visual C++, version 1.5, page 411.

Additional reference words: 7.00 1.00 1.50 2.00 2.50 KBCategory: kbprg kbprb KBSubcategory: MfcFileIO Keywords : kb16bitonly

Last Reviewed: July 23, 1997