Difference between OPEN_ALWAYS and CREATE_ALWAYS in CreateFile() of Windows API

StackOverflow https://stackoverflow.com/questions/14469607

  •  17-01-2022
  •  | 
  •  

سؤال

Can anyone explain what the difference is between the creation dispositions OPEN_ALWAYS and CREATE_ALWAYS of the CreateFile() function of the windows API?

To me it seems that they both simply 'create the file if it does not already exist'.

هل كانت مفيدة؟

المحلول

CREATE_ALWAYS also truncates the contents if the file already exists. On the other hand, OPEN_ALWAYS will not clobber an already existing file.

Here's how the different values work in tabular form:

                         |                    When the file...
This argument:           |             Exists            Does not exist
-------------------------+------------------------------------------------------
CREATE_ALWAYS            |            Truncates             Creates
CREATE_NEW         +-----------+        Fails               Creates
OPEN_ALWAYS     ===| does this |===>    Opens               Creates
OPEN_EXISTING      +-----------+        Opens                Fails
TRUNCATE_EXISTING        |            Truncates              Fails
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top