Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can we Directly write a Class Object with vectors as member into FILE ?

Can we Directly write a Class Object with vectors as member into FILE ?

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsquestion
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 Offline
    0 Offline
    002comp
    wrote on last edited by
    #1

    Hello Friends I am writing to File using FILE object Pointer using fread and fwrite functions. this is my class:

    class Test
    {
    public:
    int a;
    int b;
    std::vector c;
    };

    If I don't use vector then it is writing fine and reading too. But after using vector,its not reading properly if use fread(&obj,sizeof(Test),1,file); Do I need to Write separately vectors using std::ostream_iterator and using copy method of STL? Or is there any way If I Can Write Directly class object in single shot bcoz my class is having too many vectors as member variables. Regards Y

    P 1 Reply Last reply
    0
    • 0 002comp

      Hello Friends I am writing to File using FILE object Pointer using fread and fwrite functions. this is my class:

      class Test
      {
      public:
      int a;
      int b;
      std::vector c;
      };

      If I don't use vector then it is writing fine and reading too. But after using vector,its not reading properly if use fread(&obj,sizeof(Test),1,file); Do I need to Write separately vectors using std::ostream_iterator and using copy method of STL? Or is there any way If I Can Write Directly class object in single shot bcoz my class is having too many vectors as member variables. Regards Y

      P Offline
      P Offline
      pasztorpisti
      wrote on last edited by
      #2

      The vector contains just pointers to the actual data stored by the vector. If you write out your struct then you write out just some pointers that will probably be invalid the next time you want to load your data. The serialization method you use is the 0th solution everyone starts with and it works just with primitive types - as soon as you start using complex types like a vector you have to consider using a more sophisticated serialization method. Unfortunately serializing correctly needs more work and a serialization framework. I'm using my own reflection-like framework that is similar to AutoSerial[^] but is more lightweight. If you have reflection info / typeinfo then you can easily write different kind of serializers (binary, xml, ...) that will automatically work with all of your structs and types that have typeinfo attached. In my system I register every serializable member of a struct manually but from that point the struct is serializable as xml, binary, and so on depending on the kind of serialzers I have written. Runtime typeinfo has a bit of perofrmance penalty that matters quite rarely. Some other serializer frameworks do the dirty job with templates and bind to serializable data at compile time with better performance and the third type of serializers generate serializer classes/code for you by parsing your source code. In general I don't like code generators. Another popular serialization framework to check out is boost::serialzation[^]. A serialization framework can have additional benefits compared to a single-shot file-write: It can handle endianness problems and struct member alignment too if you compile the same serializer code on two different machines (in case of a server-client program that serializes data to send over network, or if you transfer the serialized data file to a completely different machine that has different endiannes/compiler struct member alignment).

      0 1 Reply Last reply
      0
      • P pasztorpisti

        The vector contains just pointers to the actual data stored by the vector. If you write out your struct then you write out just some pointers that will probably be invalid the next time you want to load your data. The serialization method you use is the 0th solution everyone starts with and it works just with primitive types - as soon as you start using complex types like a vector you have to consider using a more sophisticated serialization method. Unfortunately serializing correctly needs more work and a serialization framework. I'm using my own reflection-like framework that is similar to AutoSerial[^] but is more lightweight. If you have reflection info / typeinfo then you can easily write different kind of serializers (binary, xml, ...) that will automatically work with all of your structs and types that have typeinfo attached. In my system I register every serializable member of a struct manually but from that point the struct is serializable as xml, binary, and so on depending on the kind of serialzers I have written. Runtime typeinfo has a bit of perofrmance penalty that matters quite rarely. Some other serializer frameworks do the dirty job with templates and bind to serializable data at compile time with better performance and the third type of serializers generate serializer classes/code for you by parsing your source code. In general I don't like code generators. Another popular serialization framework to check out is boost::serialzation[^]. A serialization framework can have additional benefits compared to a single-shot file-write: It can handle endianness problems and struct member alignment too if you compile the same serializer code on two different machines (in case of a server-client program that serializes data to send over network, or if you transfer the serialized data file to a completely different machine that has different endiannes/compiler struct member alignment).

        0 Offline
        0 Offline
        002comp
        wrote on last edited by
        #3

        ok, I understand. But this library is very old and not compiling,one of header file is missing. Do we have any other Library for serialization Except Boost ? Thanks For your Reply. Regards Y

        P 1 Reply Last reply
        0
        • 0 002comp

          ok, I understand. But this library is very old and not compiling,one of header file is missing. Do we have any other Library for serialization Except Boost ? Thanks For your Reply. Regards Y

          P Offline
          P Offline
          pasztorpisti
          wrote on last edited by
          #4

          There are probably but I have always used my own creatures for this kind of task so I can't recommend you a good and (I guess) free solution if you don't like boost. Long ago when I checked out the available solutions and I didn't really liked them. Maybe the others can recommend you one or you can use google to search for something that meets your requirement. As a final solution you can write your own framework that is a fun way to learn something new if you have the time and interest.

          0 1 Reply Last reply
          0
          • P pasztorpisti

            There are probably but I have always used my own creatures for this kind of task so I can't recommend you a good and (I guess) free solution if you don't like boost. Long ago when I checked out the available solutions and I didn't really liked them. Maybe the others can recommend you one or you can use google to search for something that meets your requirement. As a final solution you can write your own framework that is a fun way to learn something new if you have the time and interest.

            0 Offline
            0 Offline
            002comp
            wrote on last edited by
            #5

            Thanks Dear In case of using AutoSerial, Do I need to include only AutoSerial.h ? or Something else I need to do. I didn't get from this Docs. Thanks Once Again. Regards Y

            P 1 Reply Last reply
            0
            • 0 002comp

              Thanks Dear In case of using AutoSerial, Do I need to include only AutoSerial.h ? or Something else I need to do. I didn't get from this Docs. Thanks Once Again. Regards Y

              P Offline
              P Offline
              pasztorpisti
              wrote on last edited by
              #6

              You are welcome! Unfortunately I cannot answer this as I used it only once long ago. Serialization in C++ is a difficult topic. Every solution is messy as serialization isn't really supported by the language. In my opinion it would be very useful to have a bit more detailed runtime type info support in C++ that can be used for serialization. On detailed runtime type info I mean for example auto-generated list of struct/class members, member names, enum member list/member names, ........ On top of such info (that I'm currently putting together manaully in my serialization system) it is quite easy to write reasonably simple yet effective serializers. I would give boost::serialization a try, that is a quite popular and maintained library.

              0 1 Reply Last reply
              0
              • P pasztorpisti

                You are welcome! Unfortunately I cannot answer this as I used it only once long ago. Serialization in C++ is a difficult topic. Every solution is messy as serialization isn't really supported by the language. In my opinion it would be very useful to have a bit more detailed runtime type info support in C++ that can be used for serialization. On detailed runtime type info I mean for example auto-generated list of struct/class members, member names, enum member list/member names, ........ On top of such info (that I'm currently putting together manaully in my serialization system) it is quite easy to write reasonably simple yet effective serializers. I would give boost::serialization a try, that is a quite popular and maintained library.

                0 Offline
                0 Offline
                002comp
                wrote on last edited by
                #7

                Ok, But Boost::Serialization seems to me a heavy library. It wud be better to have one own and fun to learn a new framework. Anyway, thanks Buddy For all your replies. Regards Y

                P 1 Reply Last reply
                0
                • 0 002comp

                  Ok, But Boost::Serialization seems to me a heavy library. It wud be better to have one own and fun to learn a new framework. Anyway, thanks Buddy For all your replies. Regards Y

                  P Offline
                  P Offline
                  pasztorpisti
                  wrote on last edited by
                  #8

                  You are welcome! I'm also using my own solution because its quite lightweight and provides only a few features I need in a better way. Good luck with creating your own stuff and have fun! Don't forget to take a look around to check out the source code of some existing frameworks to steal good ideas before starting out with your own stuff!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups