如何使用c ++编辑文本文件数据 [英] How to edit text file data with c++

查看:258
本文介绍了如何使用c ++编辑文本文件数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序创建一个库存项目的文本文件,其中包含总生产,剩余库存等详细信息。现在我的问题是如何编辑该文本文件与我的程序。例如,如果我错误地输入正确的数据(如生产是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 ++编辑文本文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆