简体   繁体   English

qt二进制文件读写

[英]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)打开二进制文件并写入'b'(二进制)

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读取但不是 mT='b'。如果 ch 和 mT 变量是 int 总是 mT=4 为什么?我如何编写 ch(二进制文件)并从二进制文件中读取

the 4 number is the length of your data. 4数字是数据的长度。 QDataStream store length of your data before it to indicate how many bytes need to read to gain the written data. QDataStream在它之前存储数据的长度,以指示需要读取多少字节才能获得写入的数据。 your data has been written after it.您的数据已写入它之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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