用C ++写一个文件的不同结构? [英] Writing different structs to a file in C++?

查看:181
本文介绍了用C ++写一个文件的不同结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法将三种不同类型的结构写入二进制文件,以后必须对其进行搜索。 (例如,结构A有两个字段,一个是int和一个char; struct B有一个int和一个long;我需要输出所有结构,其int等于键盘给出的结构)。

I need a way to write structures of three different kinds to a binary file, which later has to be searched. (As in, for example, struct A has two fields, an int and a char; struct B has int and a long; I need to output all structures whose int equals the one given from keyboard).

我理解如何将相同类型的结构体写入文件以及如何搜索它们,但在这里我只是迷失了,我想出的最好的事情是声明一个包含所有可能需要的字段的结构离开那些我不需要空的,但它确实感觉不对,有一个更好的方法来做到这一点。

I understand how to write structs of the same kind to a file and how to search them, but here I am just lost, best thing I came up with is declaring a struct containing all possibly needed fields and leaving the ones I don't need empty, but it really feels wrong, there HAS to be a better way to do that.

我读过二进制文件和找不到任何相关内容,大多数示例和教程都涉及编写一种数据类型。有人能指出我正确的方向吗?

I've read about binary files and could not find anything relevant, most examples and tutorials deal with writing one data type. Could anyone point me in the right direction?

编辑:我正在寻找@Jerry_coffin所谓的数据库模式,可能会使用其中一个现有的数据库系统,最好的方法。谢谢大家的建议

I am looking for what @Jerry_coffin called database mode, and will probably use one of the existing database systems for that, best way to go, probably. Thank you everybody for the suggestions

推荐答案

有两种基本方法可以处理你的数据(你还没有说过适用的方法) ):

There are two basic ways to deal with your data (and you haven't said which applies):


  1. 我认为是字处理器模型:读取文件,根据需要使用/修改数据,写下所有当用户保存时(或者可能是自动的,即使用户没有明确要求保存),数据也会退出。

  2. 我认为数据库模式是什么:你不要通常计划一次将整个文件读入内存。您可以根据需要更新文件的各个部分和部分。您构建文件以支持或多或少的随机访问。

如果您正在计划第一个,那么事情非常简单。保持三个结构在内存中分开。当你把它们写出来时,你会写下所有类型,然后写下所有类型,最后是所有第三类。您需要在某处获得足够的元数据才能正确读取数据。

If you're planning on the first, things are pretty simple. Keep your three structures separate in memory. When you write them out, you write all of one type, then all of the next, and finally all of the third. You need enough metadata somewhere to be able to read the data back in correctly.

如果你计划在第二个,它可能更复杂。沿着这条线你有几种不同的可能性:

If you're planning on the second, it's probably more complex. You have a few different possibilities along this line:


  1. 将数据分成三个单独的文件并分别搜索每个文件。我知道你说你需要一个文件,但是如果你能做到这一点,它可能是最干净的方法。

  2. 在你的源代码中,创建一个三种类型的联合,以及一个字段告诉其余类型。这基本上只是将特定结构所不需要的空间留空,但使得在外部和内部表示之间进行映射时更简单。

  3. 构建更接近完整数据库系统的东西。基本上,您需要将原始记录写入文件。然后你会想要某种索引来告诉每条记录的起始位置。然后,您可能希望添加至少一个索引以帮助快速轻松地查找特定记录。

  4. 使用现有数据库系统。 SQLite 是一个显而易见的选择。另一个不为人知的是 STXXL 。显然,SQLite为数据提供了一个SQL接口,这使得更容易支持ad-hoc查询之类的东西(如果需要)。 STXXL提供类似于标准库中容器的接口,但数据存储在磁盘上。经过严格的测试和优化;通常需要相当多的努力来匹配其性能。

  1. Divide the data into three separate files and search each one separately. I know you said you need a single file, but if you can do this, it's probably the cleanest approach.
  2. In your source code, create a union of the three types, along with a field to tell which type the rest is. This is basically just leaving the space unneeded by a particular struct empty, but making it slightly simpler to map between the external and internal representations.
  3. Build something closer to a full-blown database system. Basically, you'll want to write the raw records into the file. Then you'll want some sort of index to tell where each record starts. Then you may want to add at least one index to help find particular records quickly and easily.
  4. Use an existing database system. SQLite is one obvious choice. Another that's not as well known is STXXL. Obviously enough, SQLite provides a SQL interface to the data, which makes it much easier to support things like ad-hoc queries and such (if needed). STXXL provides an interface similar to the containers in the standard library, but with the data stored on disk. This has been pretty heavily tested and optimized; it'll typically take quite a bit of effort to match its performance.

至于哪一个更可取:它取决于。单独的文件可能是最简单的。鉴于具有最小变化的特定要求,我可能会按照2中的建议设计单一类型,如果我需要存储大量数据,可能使用STXXL。如果您可能需要的查询更加流畅,我可能会考虑使用SQLite。我会避免编写您自己的数据库系统,除非您的需求非常特定且不寻常,因此您很有可能只需极少的努力就可以为您的使用带来重大的性能提升。坦率地说,我认为这不太可能。

As to which is preferable: it depends. Separate files is probably the simplest. Given specific requirements subject to minimal change, I'd probably design a single type as suggested in 2, and if I needed to store a lot of data, probably use STXXL. If queries you might need are much more fluid, I'd probably consider SQLite. I'd avoid writing your own database system unless your needs are extremely specific and unusual, so you see a strong likelihood of minimal effort leading to a major performance improvement for your use. Frankly, I think that's pretty unlikely though.

这篇关于用C ++写一个文件的不同结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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