我应该创建一个程序将信息存储到文件中、编辑该文件中的信息并添加新信息的最佳方法是什么

发布于 2024-07-15 23:13:04 字数 447 浏览 2 评论 0原文

我即将开始一个我正在尝试做的小项目,我将创建一个 C++ 程序来将库存数据存储到一个文件中(我猜 .txt 就可以了)

  • • 项目描述 • 现有数量 • 批发成本 • 零售成本 • 日期 添加到库存

我需要能够:

• 将新记录添加到文件中 • 显示文件中的任何记录 • 更改文件中的任何记录

在开始此操作之前,是否有什么我应该知道的事情可以使此操作变得更加容易和方便? 高效...

例如,我应该尝试使用 XML 还是通过 C++ 很难使用的内容?

我从来没有真正理解最有效的方法。

就像我会搜索文件并查找括号中的内容或其他内容吗?

编辑

数据大小不应该太大。 我想你可能会说这是为了家庭作业。 我想将结构体的内容写入文件的路径中,我该怎么做?

I'm about to start on a little project i'm trying to do where I create a C++ program to store inventory data into a file ( I guess a .txt will do )

  • • Item Description • Quantity on Hand
    • Wholesale Cost • Retail Cost • Date
    Added to Inventory

I need to be able to:

• Add new records to the file
• Display any record in the file
• Change any record in the file

Is there anything I should know of before I start this that could make this much more easy & efficient...

Like for example, should I try and use XML or what that be too hard to work with via C++?

I've never really understood the most efficient way of doing this.

Like would I search through the file and look for things in brackets or something?

EDIT

The datasize shouldn't be too large. It is for homework I guess you could say. I want to write the struct's contents into a file's route, how would I go about doing that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终陌 2024-07-22 23:13:04

有很多方法。 这是为了家庭作业还是为了实际用途? 如果是用于家庭作业,则可能对您可以使用的内容有一些限制。

否则,我建议使用一些嵌入式 DBMS,例如 SQLite。 还有其他解决方案,但这将是最强大的解决方案,并且也将具有最简单的实现。

XML 也是可以接受的,并且有许多可重用的实现,但是一旦您处理数千条记录,它就会开始损失性能。 JSON 也是如此。 人们可能仍在争论 JSON 和 XML 哪一种更简单。

另一种可能性是创建一个struct并将其内容直接写入文件。 如果记录大小不是恒定的,就会变得棘手。 而且,如果记录格式发生变化,则需要重建文件。 否则,如果认真实施,该解决方案可能是性能最佳的解决方案之一。

There are many approaches. Is this for homework or for real use? If it's for homework, there are probably some restrictions on what you may use.

Otherwise I suggest some embedded DBMS like SQLite. There are others too, but this will be the most powerful solution, and will also have the easiest implementation.

XML is also acceptable, and has many reusable implementations available, but it will start loosing performance once you go into thousands of records. The same goes for JSON. And one might still debat which one is simpler - JSON or XML.

Another possibility is to create a struct and write its contents directly to the file. Will get tricky though if the record size is not constant. And, if the record format changes, the file will need to be rebuilt. Otherwise this solution could be one of the best performance-wise - if implemented carefully.

奢欲 2024-07-22 23:13:04

您能告诉我们为什么不想使用数据库引擎吗?

如果只是为了学习那么......请给我们该文件中存储数据的估计大小和访问模式(有多少用户,他们执行操作的频率等)?

挑战将是创建有效的搜索和修改代码。

对于搜索来说,它与数据结构和组织有关。
对于修改,就是如何将更新写入文件,而不将其完全读入内存,在那里更新它,然后再次将其完全写回到文件中。

Could you please enlighten us why don't you want to use a database engine for it?

If it is just for learning then.... give us please an estimated size of stored data in that file and the access pattern (how many users, how often they do it etc.)?

The challenge will be to create an efficient search and modification code.

For the search, it's about data structures and organization.
For the modification, it's how would you write updates to the file without reading it completely into memory, updating it there and then writing it again completely back to the file.

简单气质女生网名 2024-07-22 23:13:04

如果这是一个实际使用的项目,并且有可能随着时间的推移添加功能,那么从一开始就选择数据库解决方案,即使它看起来有些过头了。 我以前也走过这条路,随着时间的推移,一些小功能会被添加,在你意识到之前,你已经实现了一个数据库。 不好。 硬着头皮使用数据库。

如果这是一个学习练习,则取决于您要存储的数据量。 如果它很小,最简单的方法是将整个文件读入内存并在那里对其进行操作。 进行更改后,将整个文件写回磁盘。 如果数据太大而无法做到这一点,那么最好的办法就是使用固定大小的记录。 创建一个包含所有数据的 POD 结构(即没有指针、stl 容器等)。 然后您可以重写单个记录,而无需重写整个文件。 如果这些都不起作用,那么最好的选择是数据库解决方案。

If this is a project that will actually be used, with the potential to have features added over time, go for a database solution from the start, even if it seems overkill. I've been down this road before, small features get added over time, and before you realize it you have implemented a database. Poorly. Bite the bullet and use a database.

If this is a learning exercise, it depends on the amount of data you want to store. If it is small, the easiest thing to do is read the entire file into memory and operate on it there. When changes are made, write the entire file back out to disk. If the data is too large to do that, the next best thing is to have fixed sized records. Create a POD struct that contains all of the data (i.e., no pointers, stl containers, etc). Then you can rewrite individual records without needed to rewrite the entire file. If neither of these will work, your best bet is a database solution.

迷爱 2024-07-22 23:13:04

如果您坚持手动执行,我建议使用 JSON 而不是 XML。

另外,考虑一下sqlite。

If you insist to do it manually, I suggest JSON instead of XML.

Also, consider sqlite.

佞臣 2024-07-22 23:13:04

这听起来对于 SQLite 来说是一项完美的工作。 体积小、速度快、灵活且易于使用。

This sounds like a perfect job for SQLite. Small, fast, flexible and easy to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文