2

How can I write/read qint32 variable to QFile? I need to do this without using Data/TextStreams. Just direct read/write.

QFile* file;
qint32 a, b;
//write a to file
//b = read a
Voronkov Ivan
  • 45
  • 1
  • 7
  • Подойдет [link](https://ru.wikipedia.org/wiki/%D0%A4%D0%B0%D0%B9%D0%BB%D0%BE%D0%B2%D1%8B%D0%B9_%D0%B2%D0%B2%D0%BE%D0%B4-%D0%B2%D1%8B%D0%B2%D0%BE%D0%B4_%D0%B2_%D1%8F%D0%B7%D1%8B%D0%BA%D0%B5_%D0%A1%D0%B8)? And this one [link](http://stackoverflow.com/questions/9465727/convert-qfile-to-file) – Alexander Chernin Mar 14 '16 at 08:55
  • Do you plan to make binary read/write? Maybe QDataStream is what you are looking for. – johngull Mar 14 '16 at 08:56
  • @AlexanderChernin not sure. I have QFile with its own read/write operations. I need to use "reinterpret_cast" here, which doesnt seem to be correct idea – Voronkov Ivan Mar 14 '16 at 08:59
  • @johngull i cannot use streams of any kind here, so not – Voronkov Ivan Mar 14 '16 at 09:00
  • 1
    @VoronkovIvan, strange limitation :) Maybe you can write all you need to QByteArray and then write it to the file? – johngull Mar 14 '16 at 09:02
  • You can use QIODevice::read* and QIODevice::write* functions – Alexander Chernin Mar 14 '16 at 09:07
  • @AlexanderChernin they dont give permission to read directly in int. Only idea is: file.read(reinterpret_cast(&a), sizeof(a) ); – Voronkov Ivan Mar 14 '16 at 09:12
  • 1
    Then you have no choice except making casting. Even inside QDataStream it is realized like `if(dev->read((char*)&i, 4)!=4) ...` – johngull Mar 14 '16 at 09:49

0 Answers0