#includeint main() { QBuffer buffer; QByteArray data = "Hello, World!"; buffer.setData(data); // Writing data to the buffer buffer.open(QIODevice::WriteOnly); QByteArray newData = "Goodbye, World!"; buffer.write(newData); // Reading data from the buffer buffer.open(QIODevice::ReadOnly); QByteArray readData = buffer.readAll(); qDebug() << readData; // Output: "Goodbye, World!" return 0; }
#includeIn this example, we create a QBuffer object and open it for reading and writing. Then, we write a binary value (in this case, a magic number) to the buffer using QDataStream. Finally, we read the value back from the buffer using a separate QDataStream object. Package/library: Qt Core library.int main() { QBuffer buffer; buffer.open(QIODevice::ReadWrite); // Writing binary data to the buffer QDataStream out(&buffer); qint32 magicNumber = 0xCAFEBABE; out << magicNumber; // Reading binary data from the buffer buffer.seek(0); QDataStream in(&buffer); qint32 readMagicNumber; in >> readMagicNumber; qDebug() << hex << readMagicNumber; // Output: 0xcafebabe return 0; }