简体   繁体   中英

qt binary file writing and reading

void write(QString filename) {
  QChar ch('b');
  QFile mfile(filename);
  if (!mfile.open(QFile::WriteOnly) {
    qDebug() << "Could not open file for writing";
    return;
  }
  QDataStream out(&mfile);
  out.setVersion(QDataStream::Qt_4_8);
  out << ch;
  mfile.close();
}

open binary file and writing 'b'(binary)

void read(QString filename) {
  QFile mfile(filename);
  if (!mfile.open(QFile::ReadOnly)) {
    qDebug() << "Could not open file for reading";
    return;
  }
  QDataStream in(&mfile);
  in.setVersion(QDataStream::Qt_4_8);
  QChar mT;
  in >> mT;
  qDebug() << mT;
  mfile.close();
}

read but not mT='b'.if ch and mT variables are int always mT=4 why?How can i writing ch(binary file) and read from binary file

the 4 number is the length of your data. QDataStream store length of your data before it to indicate how many bytes need to read to gain the written data. your data has been written after it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM