muktoshuvro
muktoshuvro

Reputation: 438

How can I read any kind of File as Binary value ?

If I want to get binary value from files no matter which format they have, how may I do that ? I have tried this code but it only can read text files line by line, nothing more than that.

QFile file(QFileDialog::getOpenFileName (this, tr("Open File"),
                                         "",tr("")));
if (!file.open(QIODevice::ReadOnly ))
        return ;
int size = file.size();
qDebug()<<size;
while (!file.atEnd()) {
      QByteArray line = file.readLine();
      qDebug()<<line;

Upvotes: 2

Views: 5268

Answers (2)

user1590232
user1590232

Reputation:

You can read not only in array of char, but also into other types. For example reading structure from file:

struct Foo
{
  int bar;
  long foobar;
}
//...
Foo foo1;
file.read(foo1, sizeof(Foo));

Upvotes: 0

Nikita Krupenko
Nikita Krupenko

Reputation: 1175

Allocate a buffer to read data into and use QFile::read function. For example:

qint64 bufSize = 1024;
char *buf = new char[bufSize];
qint64 dataSize;
while (!file.atEnd()) {
    dataSize = file.read(buf, bufSize);
    /* process data */
}

Upvotes: 2

Related Questions