Quote:
|
Originally Posted by Nasimov
It's possible to ammend data within a file vs. just adding data to the beginning or the end of the file? Mean to change something in the "middle" of the file.
Thanks.
|
The standard approach is to read in the content of the file, alter it then write it back out. Generally you cannot edit the middle of the file without following this process unless the changes do not alter the number of bytes. To explain what I mean, imagine you have this in your file on disk
abcdefghijklmnop
and you want to change efg to ggffee. If you only read the middle part of the file (putting aside how you know where to read from) then tried to write the new version back at the same location in the file you would get
abcdggffeeklmnop
because what you wrote back was longer than the original and so would overwrite 'hij' as well, thus messing up the rest of the file.
If you happened to be writing back the same number of bytes e.g. 'gfe' then you could in theory write it directly over the original using fseek() to move to the correct part of the file before writing, but usually it is
read whole file
change it
write it back over the top of the original.
HTH,
Dai