-1
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

4
  • You need to open() the file for writing/reading. Commented Dec 16, 2013 at 8:43
  • Sorry i was writing code in my mind. i opened and not change problem still Commented Dec 16, 2013 at 10:54
  • what are the values returned by in.status(), and out.status() Commented Dec 16, 2013 at 13:28
  • Can you properly write your question? You just paste some code and expect people to answer in a proper way? Also, how can this ever help someone else with similar problem? Commented Aug 5, 2015 at 15:24

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.