title: Reading specific object in QDataStream and count number of objects stored
body: <p>I am writting some objects in a binary file and I would like to read them back.
  To explain you what I am trying to do, I prepared a simple example with a class User that contains the QString name and QList name of childrens. Please see the code below.</p>
  
  <pre><code>#include "QString"
  #include "QFile"
  #include "QDataStream"
  #include "qdebug.h"
  
  class User
  {
  protected:
  QString name;
  QList&lt;QString&gt; childrens;
  
  public:
  QString getName(){ return name;}
  QList&lt;QString&gt; getChildrens(){ return childrens;}
  
  void setName(QString x) {name = x;}
  void setChildrens(QList&lt;QString&gt; x) {childrens = x;}
  
  //I have no idea of how to get the number of users in "test.db"
  int countDatabase()
  {
  
  }
  
  //I would like to read the user named "pn" without putting all users in memory
  void read(QString pn)
  {
      QFile fileRead("test.db");
      if (!fileRead.open(QIODevice::ReadOnly)) {
          qDebug() &lt;&lt; "Cannot open file for writing: test.db";
          return;
      }
      QDataStream in(&amp;fileRead);
      in.setVersion(QDataStream::Qt_5_14);
      in&gt;&gt;*this;
  }
  
  
  void write()
  {
      QFile file("test.db");
      if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) {
          qDebug() &lt;&lt; "Cannot open file for writing: test.db";
          return;
      }
      QDataStream out(&amp;file);
      out.setVersion(QDataStream::Qt_5_14);
      out&lt;&lt;*this;
  }
  
  friend QDataStream &amp;operator&lt;&lt;(QDataStream &amp;out, const User &amp;t)
  {
      out &lt;&lt; t.name &lt;&lt; t.childrens;
      return out;
  }
  
  friend QDataStream &amp;operator&gt;&gt;(QDataStream &amp;in, User &amp;t)
  {
      QString inname;
      QList&lt;QString&gt; inchildrens;
      in &gt;&gt; inname &gt;&gt; inchildrens;
      t.name = inname;
      t.childrens = inchildrens;
      return in;
  }
  
  };
  
  
  ////////////////////////////////////////////////////////////////
  int main()
  {
      User u;
      u.setName("Georges");
      u.setChildrens(QList&lt;QString&gt;()&lt;&lt;"Jeanne"&lt;&lt;"Jean");
      u.write();
  
      User v;
      u.setName("Alex");
      u.setChildrens(QList&lt;QString&gt;()&lt;&lt;"Matthew");
      u.write();
  
      User w;
      w.setName("Mario"); // no children
      w.write();
  
      User to_read;
      to_read.read("Alex");
  
      qDebug()&lt;&lt;to_read.getName();
      return 0;
  }
  </code></pre>
  
  <p>I successfully write all the users I want in my binary file. However, I would like to be able, without loading everything in memory:</p>
  
  <ul>
  <li>To know how many users are stored in the binary file,</li>
  <li>To read a user by giving the name of this user.</li>
  </ul>
  
  <p>I have used until now a QDataStream and I am overloading the &lt;&lt; and >> operators for the serialization. Maybe what I want is not possible with this method. Could you please provide me some hints to succeed with QDataStream or some other methods?</p>