Search code examples
windowsfileeditflat

What is the best way to edit the middle of an existing flat file?


I have tool that creates variables for a simulation. The current workflow involves hand copying those variables into the simulation input file. The input file is a standard flat file, i.e. not binary or XML. I would like to automate the addition of the variables to the flat input file.

The variables copy over existing variables in the file, e.g.

New Variables: Length 10 Height 20 Depth 30

Old Variables: ... Weight 100 Age 20 Length 10 Height 20 Depth 30 ...

Would like to have the old variables copy over the new variable. They are 200 lines into the flat input file.

Thanks for any insights.

P.S. This is on Windows.


Solution

  • If you're stuck using flat, then you're stuck using the old fashioned way of updating them: read from original, write to temp file, either write the original row or change the data and then write that. To add data, write it to the temp file at the appropriate point; to delete data, simply don't copy it from the original file.

    Finally, close both files and rename the temp file to the original file name.

    Alternatively, it might be time to think about a little database.