How to perform file IO using QDataStream in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:Abstract

Template:ArticleMetaData

Various Functions[edit | edit source]

  • Sets the serialization byte order to bo.
in.setByteOrder(QDataStream::LittleEndian);
  • Sets the version number of the data serialization format to v
QDataStream out(file);
out.setVersion(QDataStream::Qt_4_0);


Source File[edit | edit source]

More About QDataStream: http://pepper.troll.no/s60prereleases/doc/qdatastream.html

  1. include "fileIO.h"
  2. include <QFile>
  3. include <QLabel>
  4. include <QDataStream>

fileIO::fileIO(QWidget *parent)

   : QWidget(parent)

{ QFile file("c://test.txt"); file.open(QIODevice::WriteOnly); QDataStream out(&file); // we will serialize the data into the file out << "welcome to the Qt World"; // serialize a string file.close();


QFile read("c://test.txt"); read.open(QIODevice::ReadOnly); QDataStream in(&read); // read the data serialized from the file QString str; in >> str; }

fileIO::~fileIO() {

}