views:

56

answers:

2

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.

+1  A: 

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.

John Saunders
I went with this approach. I read in the file to a temporary variable, went down to the middle of the file, changed the bit that I needed to edit, and then wrote the file out again. Seems to work fine, especially since I am replacing whole rows and not partial rows.
JustADude
A: 

For something like this I'd be looking at a simple template engine. You'd have a base template with predefined marker tokens instead of variable values and then just pass the values required to your engine along with the template and it will spit out the resultant file, all present and correct. There are a number of Open Source template engines available in Java that would meet your needs, I imagine such things are also available in your language of choice. You could even roll your own without too much difficulty.

ninesided