QDataStream allocation between members

198 Views Asked by At
class ReadFile {
  public:
    void init();
    QList<double> getData();
  private:
    QFile file;
    QDataStream read;
    double bufferFloat;
    quint32 bufferInteger
}

The idea is now that when init() is called, the a file should be opened and navigated into a location where the data starts. Now, every time getData() is called, a chunk of bytes should be read from the file.

Pseudo-code looks like this:

void ReadFile::init()
{
  file.setFileName("...");
  file.open(QIODevice::ReadOnly);
  QDataStream read(&file);

  // This chunk does some magic to find correct location by applying
  // a readout (read >> bufferInteger) and check the result

  // I can verify that at this point, if doing a readout (read >> bufferFloat)
  // I get good data (i.e. corresponding to the file).
}

and

QList<double> ReadFile::getData()
{
  // Doing a (read >> bufferFloat) at this point will result in zeros.
}

I understand why this happens, since the read in init is declared locally. But how should I allocate the data stream so getData can pick up where init left off? And the next getData can pick up where the previous left off. A call sequence would look like:

ReadFile file();
file.init();
file.readData();
file.readData();
file.readData();
file.readData();
//etc...
1

There are 1 best solutions below

1
On BEST ANSWER

There is an error in your code. This line:

QDataStream read(&file);

defines a local variable, which overrides class member. Instead you should do this:

read.setDevice(&file);