I am using winapi's CreateFile() and WriteFile() functions to open the file in append mode and write into the file. When i use FILE_APPEND_DATA flag, and then write into the file, it append the new item at the end of the file, but i want to append the new data at the beginning of the file. I have been through many forums and the official documentation of the winapi but not able to find the solution can anyone help me with it.
Asked
Active
Viewed 96 times
-1
-
10I am 99% certain that what you want does not exist - there is no way to shove new bytes into the beginning of a file without overwriting existing data. The workaround is to read all the data in the file, then write the new data followed by the old. – Frodyne Jan 30 '23 at 12:21
-
1You haven't found the solution because there isn't any. "Appending" to files means adding at the end. There are no current filesystems that support adding to files anywhere except at the end. – molbdnilo Jan 30 '23 at 12:35
-
This is impossible to achieve in a coherent way over all the different filesystems, so it can not be supported by any OS which wants to support such filesystems. For instance, a FS could store files using 1KiB blocks, hence in principle it could support insertion (at the beginning or the middle) of new 1KiB blocks. If another FS uses instead 4KiB blocks, then it can not support the same insertions. At best, the OS could support this insertion as an _optional_ feature, to be enabled only if the FS allows for it. Personally, I know of no OS that supports such optional insertion feature. – chi Jan 30 '23 at 12:38
-
See e.g. [the Cambridge dictionary](https://dictionary.cambridge.org/dictionary/english/append). – molbdnilo Jan 30 '23 at 12:40
-
Does this answer your question? [Insert bytes into middle of a file (in windows filesystem) without reading entire file (using File Allocation Table)?](https://stackoverflow.com/questions/13430210/insert-bytes-into-middle-of-a-file-in-windows-filesystem-without-reading-entir) – Raymond Chen Jan 30 '23 at 16:56
1 Answers
0
Just because the you wish that "append" were to mean "insert data at any random position" doesn't make it do that. "Appending" has an unambiguous meaning, and opening a file in FILE_APPEND_DATA
mode provides just that.
There is no system-supported way to open a file in "prepend" mode (which is apparently what you're looking for). You'll have to find another solution to your problem, or a better problem to solve.

IInspectable
- 46,945
- 8
- 85
- 181