I have a large array of floats and need to write sections of the array out to multiple files. The following code demonstrates an approach (that didn't work):

Qt Code:
  1. const int FILES = 8;
  2. QVector<QDataStream> v;
  3.  
  4. for (int i=0; i<FILES; i++)
  5. {
  6. QString qstr("f");
  7. qstr.append(QString::number(i));
  8. qstr.append(".dat");
  9. QFile f(qstr);
  10. f.open(QIODevice::WriteOnly | QIODevice::Append);
  11. QDataStream ds(&f);
  12. v.append(ds);
  13. }
  14.  
  15. for (int cnt=0; cnt<99; cnt++)
  16. {
  17. for (int i=0; i<FILES; i++)
  18. {
  19. QDataStream ds = v.at(i);
  20. ds << 999.9; // test
  21. }
  22. }
  23. for (int i=0; i<FILES; i++)
  24. {
  25. v.at(i).device().close();
  26. }
To copy to clipboard, switch view to plain text mode 

This code gives the following compiler error:

QDataStream::QDataStream(const QDataStream&) is private

So, my solution was to replace the QVector<QDataStream> with pointers to QDataStream:


Qt Code:
  1. const int FILES = 8;
  2. QVector<QDataStream *> v;
  3.  
  4. for (int i=0; i<FILES; i++)
  5. {
  6. QString qstr("f");
  7. qstr.append(QString::number(i));
  8. qstr.append(".dat");
  9. QFile f(qstr);
  10. f.open(QIODevice::WriteOnly | QIODevice::Append);
  11. QDataStream *ds = new QDataStream(&f);
  12. v.append(ds);
  13. }
  14.  
  15. for (int cnt=0; cnt<99; cnt++)
  16. {
  17. for (int i=0; i<FILES; i++)
  18. {
  19. QDataStream *ds = v.at(i);
  20. *ds << 999.9; // test
  21. }
  22. }
  23. for (int i=0; i<FILES; i++)
  24. {
  25. v.at(i)->device()->close();
  26. }
To copy to clipboard, switch view to plain text mode 


This compiled, but when it ran, the debugger stopped in qglobal.h at a line having something to do with QFlags. This is the line where it seems to break in qglobal.h:

inline QFlags operator&(Enum f) const { QFlags g; g.i = i & f; return g; }


I avoided this altogether by opening a QFile - QDataStream and writing values, then closing the stream each time within a loop. This worked but seems terribly inefficient.

Can someone demonstrate how to open and write to multiple binary files using QDataStream's operators?