3

I want to open a file for writing using CreateFile(). If the file exists, I will set the file pointer to the end and then write data using WriteFile(). If it doesn't exist, I'll write at the beggining of the file.

Should I use FILE_APPEND_DATA or should I use FILE_WRITE_DATA or maybe FILE_WRITE_DATA|FILE_APPEND_DATA in the dwDesiredAccess parameter of CreateFile()?

Isn't the writing at the end is also a writing, so why is there FILE_APPEND_DATA?

CITBL
  • 1,587
  • 3
  • 21
  • 36

2 Answers2

4

why is there FILE_APPEND_DATA?

FILE_APPEND_DATA automatically writes to the end of the file so that you do not have to call SetFilePointer/Ex() manually before writing. FILE_WRITE_DATA does not do that.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • It might be worth noting whether it moves the file pointer only at file open, or before each and every write request. – Ben Voigt Nov 20 '13 at 22:53
  • @BenVoigt: If the file pointer is at EOF, regardless of how it got there, a write is going to move the file pointer to the new EOF one way or the other. It does not make sense to do a write at EOF and NOT move the pointer, otherwise a subsequent write would just overwrite the previously written data. – Remy Lebeau Nov 20 '13 at 23:21
  • 1
    @RemyLebeau Could you please give me a link to where it is described in the Microsoft docs? That it moves the file pointer to the end automatically. I couldn't find it... – CITBL Nov 21 '13 at 08:05
  • It matters if you move the position yourself. It's perfectly possible to write and read using the same file handle, provided the right now was used when opening. Or one might want to rewind and overwrite the newly appended data. – Ben Voigt Nov 21 '13 at 15:52
  • @BenVoigt: it does NOT matter how the file pointer gets to EOF, whether manually via SetFilePointer/Ex() or automatically via FILE_APPEND_DATA. A write always advances the file pointer to the end of the written data, and if that new position exceeds the current EOF then EOF is moved to the new position. Sure, you can manually rewind the file pointer and overwrite the existing data (unless you use FILE_APPEND_DATA without FILE_WRITE_DATA, that is), but that is not the issue being discussed. – Remy Lebeau Nov 21 '13 at 17:16
  • 1
    @RemyLebeau: The difference between `FILE_WRITE_DATA` vs `FILE_APPEND_DATA` vs `FILE_WRITE_DATA | FILE_APPEND_DATA` is exactly the issue being discussed. They do have different behavior wrt automatically changing the file position. And an unambiguous description of what that behavior is would be, IMO, a useful addition. Of course it doesn't matter how the file pointer gets to EOF, but your answer doesn't say whether a write can take place other than at/beyond the end, if the user has manually called `SetFilePointer`. – Ben Voigt Nov 21 '13 at 17:55
  • The atomic behavior of `FILE_APPEND_DATA` is also a very interested property that distinguishes it from `FILE_WRITE_DATA`. – Ben Voigt Nov 21 '13 at 17:59
  • @BenVoigt: Of course a write can take place at/past EOF if `SetFilePointer/Ex()` is used. – Remy Lebeau Nov 21 '13 at 21:03
  • [citation needed], please. – Rob Kennedy Apr 15 '14 at 22:54
3

FILE_WRITE_DATA lets you write to anywhere in the file, whereas FILE_APPEND_DATA lets you append data to the file but not modify existing data. There's no point using both since FILE_WRITE_DATA lets you append as well. If all you want to do is append then FILE_APPEND_DATA is all you need.

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • One more interesting fact about `FILE_APPEND_DATA` that you can get atomic append on local files. The append behavior is properly synchronized between writes via different handles. – Noteworthy Feb 17 '21 at 07:20
  • @Noteworthy Where is this atomic behavior described in the documentation? Could you please give a link? – CITBL Jul 18 '23 at 09:59
  • @CITBL I actually based my answer on this [thread](https://stackoverflow.com/questions/3032482/is-appending-to-a-file-atomic-with-windows-ntfs), however going through it again seems like it is not guaranteed. – Noteworthy Sep 02 '23 at 03:59