如何使用c ++编辑文本文件数据

编程入门 行业动态 更新时间:2024-10-28 15:32:45
本文介绍了如何使用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 ++编辑文本文件数据

本文发布于:2023-07-09 00:17:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1082109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文本文件   编辑   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!