Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to read binary file
Forum Updated to NodeBB v4.3 + New Features

How to read binary file

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 78.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Verminoz
    wrote on 7 Jun 2011, 19:58 last edited by
    #1

    Hi,

    I'm trying to read a file as a binary, byte-by-byte, and strangely nothing seems to work with QFile. This code doesn't work but I 've also tried data streams and readAll(). Every time it gets truncated.

    @
    QFile file(fileName);
    char* temp;
    //int i;

    if (!file.open(QIODevice::ReadOnly)) {       
        return ;
    }
    
    temp = new char [file.size()];
    file.read(temp,file.size());
    
    file.close();
    QByteArray blob(temp);
    
    qDebug() << file.size() << endl;
    qDebug() << "----------------------------" << endl;
    qDebug() << blob << endl;
    qDebug() << "----------------------------" << endl;
    qDebug() << file.error() << endl;
    

    @

    I keep getting something like this

    @
    89989


    "ÿØÿà"


    0
    @

    It works with text files but I want it to work with all files, eg. jpg,png,pdf etc.

    I don't care about the file format. I just want a binary copy.

    Ohne Muzik ware das Leben ein Irrtum.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on 7 Jun 2011, 20:36 last edited by
      #2

      Why the temp?

      @
      QFile file(fileName);
      if (!file.open(QIODevice::ReadOnly)) return;
      QByteArray blob = file.readAll();
      @

      should get you the contents of the file into your blob. That is what you were doing as well but without the unnecessary char array.

      Your code fails in line 13 where the blob is constructed from the temp char array. The constructor you are using is meant to "adopt" a char *, so it copies all the data starting at temp[0] up to the first 0 byte found. As you probably know a 0 byte signifies the end of a string with C character arrays.

      1 Reply Last reply
      0

      2/2

      7 Jun 2011, 20:36

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved