1. 程式人生 > >利用QDataStream將大檔案轉化成二進位制檔案QBatyArray

利用QDataStream將大檔案轉化成二進位制檔案QBatyArray

用QDataStream的readRawData()函式讀取二進位制的檔案,檔案過大時會造成程式崩潰,我們可以多次讀取檔案,然後將資料塊組合成整體的QByteArray

  QByteArray total_file;
  QFile file("D:/jpeg.jpg");
  int flag=file.open(QIODevice::ReadOnly);
  QDataStream in(&file);
  QFileInfo fileInfo("D:/jpeg.jpg");
  int buffer_size=65535;
  char datRawArray[buffer_size]
; qDebug()<<"nomal"<<fileInfo.size()<<endl; int total_size=0; while(!in.atEnd()){ in.readRawData(datRawArray, buffer_size); QByteArray block(datRawArray,buffer_size); total_file=total_file+block; } qDebug()<<"read file finished,yotal size:"<<total_file.
size()<<endl;