我有一个程序创建一个库存项目的文本文件,其中包含总生产,剩余库存等详细信息。现在我的问题是如何编辑该文本文件与我的程序。例如,如果我错误地输入正确的数据(如生产是500件,但只输入400)现在我如何编辑我的文件,使其正确,而不影响其他数据。
I have a program that create a text file of stock items, which contains detail of 'total production' , 'stock remaining' and so on. Now my question is how do I edit that text file with my program. For example if I mistake to enter a correct data (like production was 500 pieces but enter only 400) now how can I edit my file to make it correct without effecting other data.
推荐答案你可能不应该首先创建一个文本文件。您是否考虑使用 sqlite (或索引文件àla GDBM ...)或某些真实数据库,如 PostgreSQL 或 MongoDb ?
You probably should not create a text file in the first place. Did you consider using sqlite (or indexed files à la GDBM ...) or some real database like PostgreSQL or MongoDb?
如果您坚持以编程方式编辑文本文件,唯一的办法是处理每一行:保持所有的内存,或者将它们(除了你要改变的)复制到一些新的文件....但没有可移植的方式来改变一个
If you insist on editing programmatically a textual file, the only way is to process every line : either keep all of them in memory, or copy them (except the one you'll change) to some new file.... But there is no portable way to change the content of a file in the middle.
您可能还对文字序列化格式,例如 JSON , YAML (甚至可能是XML)。
You might also be interested in textual serialization formats like JSON, YAML (or maybe even XML).
更多推荐
如何使用c ++编辑文本文件数据
发布评论