编辑现有平面文件中间的最佳方法是什么?
我有为模拟创建变量的工具。 当前的工作流程涉及将这些变量手动复制到模拟输入文件中。 输入文件是标准平面文件,即不是二进制文件或XML。 我想自动将变量添加到平面输入文件中。
变量复制文件中的现有变量,例如
新变量: 长度10 身高20 深度 30
旧变量: ... 重量100 20岁 长度10 身高20 深度30 ...
希望将旧变量复制到新变量上。 它们是平面输入文件中的 200 行。
感谢您的任何见解。
PS这是在Windows上。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您坚持使用平面,那么您就会坚持使用老式的更新方式:从原始文件读取,写入临时文件,要么写入原始行,要么更改数据然后写入。 要添加数据,请将其写入临时文件中的适当位置; 要删除数据,只需不要从原始文件中复制它即可。
最后,关闭这两个文件并将临时文件重命名为原始文件名。
或者,也许是时候考虑一个小型数据库了。
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.
对于这样的事情,我会考虑一个简单的模板引擎。 您将有一个带有预定义标记标记而不是变量值的基本模板,然后只需将所需的值与模板一起传递给引擎,它就会吐出结果文件,所有文件都存在且正确。 有许多 Java 中可用的开源模板引擎可以满足您的需求需要,我想这些东西也可以用您选择的语言提供。 您甚至可以自己推出,没有太大困难。
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.
请注意,在 Unix 下,人们可能会考虑使用
mmap()
,因为您可以使用memmove()
等函数来移动数据并添加新数据或 < code>truncate() 如果文件较小则结果(您可能还想使用truncate()
来增大文件)。在 MS-Windows 下,您有
MapViewOfFileEx()
函数做同样的事情。 但 API 有所不同,我不太确定会发生什么或如何增大/缩小文件(MSDN 还包括类似 truncate() 的函数,也许可以工作)。
当然,正确使用
memcpy()
或memmove()
很重要,以免覆盖错误的数据或超出缓冲区。Note that under Unix, one would probably look at using
mmap()
because you can then use functions such asmemmove()
to move the data around and add new data ortruncate()
the result if the file is then smaller (you may also want to usetruncate()
to grow the file).Under MS-Windows, you have the
MapViewOfFileEx()
function to do the same thing. The API is different, though,and I'm not exactly sure what happens or how to grow/shrink the file (MSDN also includes a
truncate()
-like function and maybe that works).Of course, it's important to use
memcpy()
ormemmove()
properly to not overwrite the wrong data or go outside the buffer.