FILE_WRITE_DATA 和 FILE_APPEND_DATA 的区别 [英] Difference between FILE_WRITE_DATA and FILE_APPEND_DATA
问题描述
我想使用 CreateFile() 打开一个文件进行写入.如果文件存在,我会将文件指针设置到末尾,然后使用 WriteFile() 写入数据.如果它不存在,我会在文件的开头写.
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.
我应该在 CreateFile() 的 dwDesiredAccess 参数中使用 FILE_APPEND_DATA 还是应该使用 FILE_WRITE_DATA 或者 FILE_WRITE_DATA|FILE_APPEND_DATA?
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()?
最后的写法不也是写法,为什么会有FILE_APPEND_DATA?
Isn't the writing at the end is also a writing, so why is there FILE_APPEND_DATA?
推荐答案
FILE_WRITE_DATA
可让您写入文件中的任何位置,而 FILE_APPEND_DATA
可让您将数据附加到文件但不能修改现有数据.由于 FILE_WRITE_DATA
也允许您附加,因此两者都没有意义.如果您只想追加,那么 FILE_APPEND_DATA
就是您所需要的.
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.
这篇关于FILE_WRITE_DATA 和 FILE_APPEND_DATA 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!