c/c++语言开发共享用C ++写一个文件的不同结构?

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

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

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

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

    处理您的数据有两种基本方法(您还没有说明哪种方法适用):

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

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

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

    我建议使用protobuf

    例如,假设您创建了以下protobuf消息。

    message binary_info { message struct_1 { required int32 intValue = 1; required string strValue = 2; } message struct_2 { required int32 intValue = 1; required uint64 ulongValue = 2; } message struct_3 { required int32 intValue = 1; required uint64 ulongValue = 2; required string strValue = 3; } required struct_1 st1 = 1; required struct_2 st2 = 2; required struct_3 st3 = 3; } 

    让google protobuf为您创建必要的文件。 请参阅https://developers.google.com/protocol-buffers/docs/overview上的开发者指南

    (在我的例子中,protoc在communications / proto / *上自动生成了message.pb.h和message.pb.cc文件)

    然后,您可以编写一个程序来保存和从文件读取数据

     /* * main.cpp * * Created on: 30/05/2014 * Author: ankit */ #include "communications/proto/message.pb.h" #include  #include  using namespace std; bool write_data() { binary_info_struct_1* s1 = new binary_info_struct_1(); s1->set_intvalue(1); s1->set_strvalue("string for s1"); binary_info_struct_2* s2 = new binary_info_struct_2(); s2->set_intvalue(2); s2->set_ulongvalue(2000); binary_info_struct_3* s3 = new binary_info_struct_3(); s3->set_intvalue(3); s3->set_ulongvalue(3000); s3->set_strvalue("string for s3"); binary_info b; b.set_allocated_st1(s1); b.set_allocated_st2(s2); b.set_allocated_st3(s3); if(!b.IsInitialized()) return false; fstream output("myfile.data", ios::out | ios::binary); b.SerializeToOstream(&output); return true; } void read_data() { fstream input("myfile.data", ios::in | ios::binary); binary_info b; b.ParseFromIstream(&input); cout << "struct 1, int data: " << b.st1().intvalue() << endl; cout << "struct 1, string data: " << b.st1().strvalue() << endl; cout << "struct 2, int data: " << b.st2().intvalue() << endl; cout << "struct 2, ulong data: " << b.st2().ulongvalue() << endl; cout << "struct 3, int data: " << b.st3().intvalue() << endl; cout << "struct 3, ulong data: " << b.st3().ulongvalue() << endl; cout << "struct 3, string data: " << b.st3().strvalue() << endl; } int main() { write_data(); read_data(); return 0; } 

    希望这可以帮助!

      以上就是c/c++开发分享用C ++写一个文件的不同结构?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/522799.html

      (0)
      上一篇 2020年12月11日
      下一篇 2020年12月11日

      精彩推荐