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

现在的想法是,当调用 init() 时,应该打开文件并导航到数据开始的位置。现在,每次调用 getData() 时,都应该从文件中读取一大块字节。

伪代码如下所示:

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).
}

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

我理解为什么会发生这种情况,因为readininit是在本地声明的。但是我应该如何分配数据流以便从中断getData的地方init继续?下一个getData可以从上一个中断的地方继续。调用序列如下所示:

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

1 回答 1

2

您的代码中有错误。这一行:

QDataStream read(&file);

定义一个覆盖类成员的局部变量。相反,您应该这样做:

read.setDevice(&file);
于 2015-06-02T14:33:58.660 回答