Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with gbinary files, and QVector
Forum Updated to NodeBB v4.3 + New Features

Problem with gbinary files, and QVector

Scheduled Pinned Locked Moved General and Desktop
35 Posts 7 Posters 5.7k Views 2 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.
  • Christian EhrlicherC Christian Ehrlicher

    @Christian-Ehrlicher said in Problem with gbinary files, and QVector:

    I don't think you want to read a file which gets truncated when it's opened for reading.

    Did you actually read what ios_base::trunc does?

    L Offline
    L Offline
    Loc888
    wrote on last edited by
    #12

    @Christian-Ehrlicher I remember i tried not using it, and it dosn't help.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #13

      You're truncating your file both when reading and writing. Then you have that data variable but it's unknown whether it's properly initialised.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        You're truncating your file both when reading and writing. Then you have that data variable but it's unknown whether it's properly initialised.

        L Offline
        L Offline
        Loc888
        wrote on last edited by Loc888
        #14

        @SGaist Ok, i try again, but take in consideration in those tutorials i saw, they use that trunc.
        I removed that trunc everywhere, now something is working, but the next problem i get, is it's crashing, because of the string "name" variable.
        What happened, is i get the error, i run the debugger, and it points on the name variable, in the show_data function, if i just comment it, then it's working.

        How to fix this? I guess it's the stream size fault, because last day i saw a weird error, and saw some weird symbols when i was trying to read the data, and the program crash few seconds later.

        How to save all those QVectors, stings etc, when the size variable change?

        mrjjM 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #15

          I do take that into consideration. You should also take into consideration that if there's something you are not sure about, you should look up the documentation regarding that element rather than blindingly adding things here and there.

          As suggested before, since you are using Qt, use its facilities like QFile and QDataStream and be done with it. Streaming of QString, QVector etc, is already supported.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 1 Reply Last reply
          3
          • SGaistS SGaist

            I do take that into consideration. You should also take into consideration that if there's something you are not sure about, you should look up the documentation regarding that element rather than blindingly adding things here and there.

            As suggested before, since you are using Qt, use its facilities like QFile and QDataStream and be done with it. Streaming of QString, QVector etc, is already supported.

            L Offline
            L Offline
            Loc888
            wrote on last edited by
            #16

            @SGaist I don't know any documentation about files and arrays, if i was able to do it with that, i would not post anything, because there is no point.

            aha_1980A 1 Reply Last reply
            0
            • L Loc888

              @SGaist I don't know any documentation about files and arrays, if i was able to do it with that, i would not post anything, because there is no point.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #17

              @Loc888 The link to QDataStream that @SGaist gave you contains an almost reusable, yet small example for loading and saving. Have you looked at it?

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              1
              • L Loc888

                @SGaist Ok, i try again, but take in consideration in those tutorials i saw, they use that trunc.
                I removed that trunc everywhere, now something is working, but the next problem i get, is it's crashing, because of the string "name" variable.
                What happened, is i get the error, i run the debugger, and it points on the name variable, in the show_data function, if i just comment it, then it's working.

                How to fix this? I guess it's the stream size fault, because last day i saw a weird error, and saw some weird symbols when i was trying to read the data, and the program crash few seconds later.

                How to save all those QVectors, stings etc, when the size variable change?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #18

                @Loc888
                Hi
                The real benefit of using QFile and QDataStream is that it already can handle many Qt types.
                (including QString and QVector)

                http://doc.qt.io/qt-5/datastreamformat.html

                So you need not to do anything special besides how mini sample shows it

                QFile file("file.dat");
                file.open(QIODevice::WriteOnly);
                QDataStream out(&file); // we will serialize the data into the file
                QVector<double> list;
                out << list; // serialize a list of doubles. handle size by it self.

                However, if you QVector is a list of custom object ( your own class) you need to define
                << and >> for it so it know how to stream your types too.
                Often that is just to stream the member variables if they in turn are plain Qt types.

                L 1 Reply Last reply
                2
                • mrjjM mrjj

                  @Loc888
                  Hi
                  The real benefit of using QFile and QDataStream is that it already can handle many Qt types.
                  (including QString and QVector)

                  http://doc.qt.io/qt-5/datastreamformat.html

                  So you need not to do anything special besides how mini sample shows it

                  QFile file("file.dat");
                  file.open(QIODevice::WriteOnly);
                  QDataStream out(&file); // we will serialize the data into the file
                  QVector<double> list;
                  out << list; // serialize a list of doubles. handle size by it self.

                  However, if you QVector is a list of custom object ( your own class) you need to define
                  << and >> for it so it know how to stream your types too.
                  Often that is just to stream the member variables if they in turn are plain Qt types.

                  L Offline
                  L Offline
                  Loc888
                  wrote on last edited by
                  #19

                  @mrjj Ok, i tried it with simple data, and it's working very good, it's even too easy to use, with comparision to that std stuff. So, about that standard method, if someone could help me learn how to do that, i mean how to read an array i will appreciate it, even if i am gonna use QFile because it's much more cleaner and faster too use.

                  mrjjM 1 Reply Last reply
                  0
                  • L Loc888

                    @mrjj Ok, i tried it with simple data, and it's working very good, it's even too easy to use, with comparision to that std stuff. So, about that standard method, if someone could help me learn how to do that, i mean how to read an array i will appreciate it, even if i am gonna use QFile because it's much more cleaner and faster too use.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #20

                    @Loc888
                    Hi
                    yes, its actually nicer than the std stuff.

                    Well, it can directly do it with QVector.
                    It actually write the size first then each item but its handled automatically.

                    When you say array, you mean QVector or what type ?

                    for any supported Qtype, a list or vector is the same as basic variables.
                    simply
                    outstream << thelist;
                    and
                    instream >> somelistvar;

                    Do you have a special case in in mind with the "array" ?
                    Can you show the declaration of your array ?
                    makes it easier to talk about.

                    L 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @Loc888
                      Hi
                      yes, its actually nicer than the std stuff.

                      Well, it can directly do it with QVector.
                      It actually write the size first then each item but its handled automatically.

                      When you say array, you mean QVector or what type ?

                      for any supported Qtype, a list or vector is the same as basic variables.
                      simply
                      outstream << thelist;
                      and
                      instream >> somelistvar;

                      Do you have a special case in in mind with the "array" ?
                      Can you show the declaration of your array ?
                      makes it easier to talk about.

                      L Offline
                      L Offline
                      Loc888
                      wrote on last edited by
                      #21

                      @mrjj Back to that QFile for one moment, i have to inherit that QDataStream, and overload the operators?

                      jsulmJ 1 Reply Last reply
                      0
                      • L Loc888

                        @mrjj Back to that QFile for one moment, i have to inherit that QDataStream, and overload the operators?

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #22

                        @Loc888 No, you just implement << and >> operators for your own data types.
                        See documentation: http://doc.qt.io/qt-5/qdatastream.html#
                        You need:

                        QDataStream &operator<<(QDataStream &, const YOUR_TYPE &);
                        QDataStream &operator>>(QDataStream &, YOUR_TYPE &);
                        

                        You don't have to do it for Qt types like QVector.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        L 1 Reply Last reply
                        3
                        • jsulmJ jsulm

                          @Loc888 No, you just implement << and >> operators for your own data types.
                          See documentation: http://doc.qt.io/qt-5/qdatastream.html#
                          You need:

                          QDataStream &operator<<(QDataStream &, const YOUR_TYPE &);
                          QDataStream &operator>>(QDataStream &, YOUR_TYPE &);
                          

                          You don't have to do it for Qt types like QVector.

                          L Offline
                          L Offline
                          Loc888
                          wrote on last edited by
                          #23

                          @jsulm Ok, i took a look, and it says

                          "In addition to the overloaded stream operators documented here, any Qt classes that you might want to serialize to a QDataStream will have appropriate stream operators declared as non-member of the class:"

                          So i have to do that, outside the class, but how to use it then? If i crate a object, then how to use that overloaded operators?

                          mrjjM 1 Reply Last reply
                          0
                          • L Loc888

                            @jsulm Ok, i took a look, and it says

                            "In addition to the overloaded stream operators documented here, any Qt classes that you might want to serialize to a QDataStream will have appropriate stream operators declared as non-member of the class:"

                            So i have to do that, outside the class, but how to use it then? If i crate a object, then how to use that overloaded operators?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #24

                            @Loc888 said in Problem with gbinary files, and QVector:

                            So i have to do that, outside the class, but how to use it then? If i crate a object, then how to use that overloaded operators?

                            Hi
                            That is the easy part.
                            Simply try it and see

                            QFile file("file.dat");
                            file.open(QIODevice::WriteOnly);
                            QDataStream out(&file);
                            out << *yourclassptr;
                            (note the *)
                            if Qt can find an << for a type, it will simply use it.

                            That way, you can << anything you like.

                            L 1 Reply Last reply
                            1
                            • mrjjM mrjj

                              @Loc888 said in Problem with gbinary files, and QVector:

                              So i have to do that, outside the class, but how to use it then? If i crate a object, then how to use that overloaded operators?

                              Hi
                              That is the easy part.
                              Simply try it and see

                              QFile file("file.dat");
                              file.open(QIODevice::WriteOnly);
                              QDataStream out(&file);
                              out << *yourclassptr;
                              (note the *)
                              if Qt can find an << for a type, it will simply use it.

                              That way, you can << anything you like.

                              L Offline
                              L Offline
                              Loc888
                              wrote on last edited by
                              #25

                              @mrjj Ok, i try. SO i have to do a class, put there thise operators, and then use:

                              out << *new_class ?

                              mrjjM 1 Reply Last reply
                              0
                              • L Loc888

                                @mrjj Ok, i try. SO i have to do a class, put there thise operators, and then use:

                                out << *new_class ?

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by mrjj
                                #26

                                @Loc888
                                Yes and the reason for the * in from of object pointer is that the header says
                                (QDataStream &, const YOUR_TYPE &); << reference

                                so if you give it the pointer without *, it
                                will save/stream the address of the object as it then matches << for int instead.
                                so say always use * when you have a pointer to an object with <<

                                1 Reply Last reply
                                0
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #27

                                  Hi
                                  Just to be sure
                                  can you show your
                                  QDataStream &operator<<(QDataStream &, const YOUR_Class &);
                                  implementation.
                                  (not the .h declaration) but the actual body of it.

                                  L 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    Hi
                                    Just to be sure
                                    can you show your
                                    QDataStream &operator<<(QDataStream &, const YOUR_Class &);
                                    implementation.
                                    (not the .h declaration) but the actual body of it.

                                    L Offline
                                    L Offline
                                    Loc888
                                    wrote on last edited by Loc888
                                    #28

                                    @mrjj In the QDataStream Class it looks like that, so i guess i am gonna use this template, except i am gonna use a custom data:

                                    inline QDataStream &QDataStream::operator<<(quint16 i)
                                    { return *this << qint16(i); }

                                    1 Reply Last reply
                                    0
                                    • mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by mrjj
                                      #29

                                      Hi
                                      Here is small sample.

                                      --in the .H file--
                                      class MyClass
                                      {
                                      public:
                                          int value = 6;
                                          QString name = "test";
                                      };
                                      
                                      QDataStream &operator<<(QDataStream &out, const MyClass &);
                                      QDataStream &operator>>(QDataStream &in, MyClass &);
                                      
                                      -----------
                                      --in the .CPP file--
                                      QDataStream &operator<<(QDataStream &out, const MyClass &classRef) {
                                      return out << classRef.value <<  classRef.name; // save each member of your class
                                      }
                                      
                                      QDataStream &operator>>(QDataStream &in,  MyClass &classRef){
                                      return in >> classRef.value >>  classRef.name; // read in IN SAME order
                                      }
                                      
                                      ------------------------------
                                      //--test of saving--
                                         {
                                              QFile file("e:/file.dat");
                                              file.open(QIODevice::WriteOnly);
                                              QDataStream out(&file);
                                              MyClass *ptr = new MyClass;
                                              out << *ptr;
                                          }
                                      //--test of loading--
                                          {
                                              QFile file("e:/file.dat");
                                              file.open(QIODevice::ReadOnly);
                                              QDataStream in(&file);
                                              MyClass *otherptr = new MyClass;
                                              in >> *otherptr;      
                                          }
                                      
                                      
                                      L 1 Reply Last reply
                                      1
                                      • mrjjM mrjj

                                        Hi
                                        Here is small sample.

                                        --in the .H file--
                                        class MyClass
                                        {
                                        public:
                                            int value = 6;
                                            QString name = "test";
                                        };
                                        
                                        QDataStream &operator<<(QDataStream &out, const MyClass &);
                                        QDataStream &operator>>(QDataStream &in, MyClass &);
                                        
                                        -----------
                                        --in the .CPP file--
                                        QDataStream &operator<<(QDataStream &out, const MyClass &classRef) {
                                        return out << classRef.value <<  classRef.name; // save each member of your class
                                        }
                                        
                                        QDataStream &operator>>(QDataStream &in,  MyClass &classRef){
                                        return in >> classRef.value >>  classRef.name; // read in IN SAME order
                                        }
                                        
                                        ------------------------------
                                        //--test of saving--
                                           {
                                                QFile file("e:/file.dat");
                                                file.open(QIODevice::WriteOnly);
                                                QDataStream out(&file);
                                                MyClass *ptr = new MyClass;
                                                out << *ptr;
                                            }
                                        //--test of loading--
                                            {
                                                QFile file("e:/file.dat");
                                                file.open(QIODevice::ReadOnly);
                                                QDataStream in(&file);
                                                MyClass *otherptr = new MyClass;
                                                in >> *otherptr;      
                                            }
                                        
                                        
                                        L Offline
                                        L Offline
                                        Loc888
                                        wrote on last edited by
                                        #30

                                        @mrjj Something weird is happening, i declared those operators in a header file, then include it, and it's working, if i use that MyClass pointer, then i get the error..

                                        mrjjM 1 Reply Last reply
                                        0
                                        • L Loc888

                                          @mrjj Something weird is happening, i declared those operators in a header file, then include it, and it's working, if i use that MyClass pointer, then i get the error..

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #31

                                          @Loc888
                                          What error?

                                          L 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved