Search code examples
pascalfreepascallazarus

How can I write to the beginning of a file and insert rather than overwrite data?


Is there a way in Pascal to write to the start of a file, without overwriting other data?

I have learnt Seek(F, 0);but this overwrites the first thing in the file.


Solution

  • You may achieve your goal with the following algorithm:

    • Create temporary file;
    • Write your new data to this temporary file;
    • Read all data from target file and append them to temporary file;
    • Rename temporary file to target one;